品尝饮料java代码,基于java的饮品售卖系统
Java新建一个饮料类Drink?
介于进了好多个问题,都看到楼下,我就过来凑个回答数。
在北京等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站设计制作、网站制作 网站设计制作按需网站建设,公司网站建设,企业网站建设,品牌网站设计,成都全网营销,成都外贸网站建设公司,北京网站建设费用合理。
public class Drink{
private String name;
public Drink() {}
public Drink(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void print() {
System.out.println(" I am " + name);;
}
public static void main(String[] args) {
Drink drink = new Drink("可口可乐");
System.out.println(Drink.print());
}
}
Java代码怎么写:有20块钱买汽水,1块一瓶,三盖子换一瓶,两空瓶换一瓶子,共能买多少汽水?
正确的结果:对兑了17次,共换了93个瓶子,第18次对兑时余下1个空瓶,2个瓶盖。
递归模型楼上“lzs丶灬 ”同志是正确的,但存在逻辑错误。我帮他修正如下:
public static int test(int sum, int pingzi, int gaizi){
if (pingzi 2 gaizi 3)
{
return sum;
}
int newPingzi = pingzi / 2;
int newGaizi = gaizi / 3;
sum += newPingzi;
sum += newGaizi;
pingzi = newPingzi + newGaizi + pingzi % 2;
gaizi = newGaizi + newPingzi + gaizi % 3;
return test(sum, pingzi, gaizi);
}
求解各位高手:用java解决品尝饮料问题
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
public class Question {
class Drink{
protected String type;//苏打,果汁?
protected String name;//名字?
protected double price;//价格
public String getType() {
return type;
}
public double getPrice() {
return price;
}
public String getName() {
return name;
}
}
class Cole extends Drink{
public Cole(){
name = "可口可乐";
type = "苏打";
price = 6.0d;
}
}
class Putaoduo extends Drink{
public Putaoduo(){
name = "葡萄多";
type = "果汁";
price = 3.0d;
}
}
class Person{
private double money;
private ListDrink drinks = new ArrayListDrink();
public void buyDrink(Drink drink) throws QianBuGouException, Exception{
if(drink != null){
double price = drink.getPrice();
if(money = price){
drinks.add(drink);
if(price == 0){
throw new Exception("竟然有免费的饮料,好爽呀!");
}else if(price 0){
throw new Exception("竟然有倒贴的饮料,好爽呀!");
}
money -= price;
}else{
throw new QianBuGouException();
}
}
}
public void drink(Drink drink) throws DrinkNotFoundException{
if(drinks.contains(drink)){
drinks.remove(drink);
}else{
throw new DrinkNotFoundException("这瓶饮料不是我的!");
}
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public ListDrink getDrinks() {
return drinks;
}
}
class QianBuGouException extends Exception{
public QianBuGouException(){
super("我的钱不够买饮料的啦,再买就破产了!");
}
}
class DrinkNotFoundException extends Exception{
public DrinkNotFoundException(String message){
super(message);
}
}
public Question(){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Person you = new Person();
you.setMoney(10d);
Cole cole = new Cole();
try{
you.drink(cole);
JOptionPane.showMessageDialog(null, "我喝了一瓶饮料", "提示信息", JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);
}
try{
you.buyDrink(new Cole());
JOptionPane.showMessageDialog(null, "我买了一瓶饮料", "提示信息", JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);
}
try{
you.drink(new Cole());
JOptionPane.showMessageDialog(null, "我喝了一瓶饮料", "提示信息", JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);
}
try{
you.buyDrink(new Putaoduo());
JOptionPane.showMessageDialog(null, "我买了一瓶饮料", "提示信息", JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);
}
try{
you.buyDrink(new Putaoduo());
JOptionPane.showMessageDialog(null, "我买了一瓶饮料", "提示信息", JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);
}
try{
you.buyDrink(new Putaoduo());
JOptionPane.showMessageDialog(null, "我买了一瓶饮料", "提示信息", JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);
}
try{
you.buyDrink(new Putaoduo());
JOptionPane.showMessageDialog(null, "我买了一瓶饮料", "提示信息", JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);
}
ListDrink drinks = you.getDrinks();
int all = drinks.size();
for(int i = 0; i all; i ++){
Drink drink = drinks.get(0);
try{
you.drink(drink);
JOptionPane.showMessageDialog(null, "我喝了一瓶饮料", "提示信息", JOptionPane.ERROR_MESSAGE);
}catch(Exception e){
JOptionPane.showMessageDialog(null, e.getMessage(), "提示信息", JOptionPane.ERROR_MESSAGE);
}
}
JOptionPane.showMessageDialog(null, "我还有" + drinks.size() + "瓶饮料。", "提示信息", JOptionPane.ERROR_MESSAGE);
}
});
}
public static void main(String args[]){
new Question();
}
}
用java模拟饮料自动售卖工作程序,如价格已知,投入高于它本事价格自动计算找零,低于会提示请继续投
下附代码,可以在键盘输入加入判断,比如是否输入的是数字而不是字母等等,是否输入的是正数而不是其他符号... 另外可以将下面代码优化,封装,在main方法只做调用即可
package test;
import java.util.Scanner;
public class test {
public static void main(String[] args) {
boolean flag = true;
while(flag){
System.out.println("欢迎使用自动饮料售卖机");
System.out.println("1.雪碧 3.00");
System.out.println("2.可乐 3.00");
System.out.println("3.特仑苏 5.00");
System.out.println("4.农夫山泉 2.00");
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.print("请输入你的选择是:");
int choise = input.nextInt();
System.out.print("请投币:");
int money = input.nextInt();
money(money, choise);
System.out.print("是否继续'1.继续 0.退出' :");
int goon = input.nextInt();
if(goon==1){
flag = true;
}else{
flag = false;
}
}
}
public static void money(int money,int choise){
switch(choise){
case 1:
if(money-30){
System.out.println("找零"+(money-3)+"元");
}else if(money-30){
System.out.println("请再投入"+(3-money)+"元");
}else if(money-3==0){
System.out.println("生活愉快");
}else{
System.out.println("输入有误!");
}
break ;
case 2:
if(money-30){
System.out.println("找零"+(money-3)+"元");
}else if(money-30){
System.out.println("请再投入"+(3-money)+"元");
}else if(money-3==0){
System.out.println("生活愉快");
}else{
System.out.println("输入有误!");
}
break ;
case 3:
if(money-50){
System.out.println("找零"+(money-5)+"元");
}else if(money-50){
System.out.println("请再投入"+(5-money)+"元");
}else if(money-5==0){
System.out.println("生活愉快");
}else{
System.out.println("输入有误!");
}
break ;
case 4:
if(money-20){
System.out.println("找零"+(money-2)+"元");
}else if(money-20){
System.out.println("请再投入"+(2-money)+"元");
}else if(money-2==0){
System.out.println("生活愉快");
}else{
System.out.println("输入有误!");
}
break ;
}
}
}
网站栏目:品尝饮料java代码,基于java的饮品售卖系统
网址分享:http://pwwzsj.com/article/dseiigs.html