大家看下这个简单的程序为什么不能输出理想的结果!
public class StarBuzzCoffee {
Scanner scan=new Scanner(System.in);
public static void main(String[] args){
Beverage b = null;
StarBuzzCoffee sc=new StarBuzzCoffee();
sc.chooseCoffee(b);
}
//选择咖啡
public void chooseCoffee(Beverage b){
System.out.println("請選擇:");
System.out.println("1--> Empresso");
System.out.println("2--> HousBlend");
System.out.println("-----------------------");
int i=scan.nextInt();
if(i==1){
b=new Espresso();
chooseCondiment(b);
}
if(i==2){
b=new HousBlend();
chooseCondiment(b);
}
else{
System.out.println("-----------------------");
System.out.println("對不起!沒有你選擇的coffee");
chooseCoffee(b);
}
}
//调料选择 public void chooseCondiment(Beverage b){
System.out.println("-----------------------");
System.out.println("請選擇調料:");
System.out.println("1--> Mocha");
System.out.println("2--> Milk");
System.out.println("3--> Don't Anything");
System.out.println("-----------------------");
// Scanner scan=new Scanner(System.in);
int i=scan.nextInt();
if(i==1){
b=new Mocha(b);
accounts(b);
}
if(i==2){
b=new Milk(b);
accounts(b);
}
else{
System.out.println("你好!你购买的coffee是:");
System.out.println(b.getDescription());
System.out.println("总共是:" + b.cost());
}
}
//结账
public void accounts(Beverage b){
System.out.println("-----------------------");
System.out.println("是否继续选择调料");
System.out.println("1--> yes");
System.out.println("2--> no");
System.out.println("-----------------------");
// Scanner scan=new Scanner(System.in);
int i=scan.nextInt();
if(i==1){
chooseCondiment(b);
}
else{
System.out.println("你好!你购买的coffee是--:");
System.out.println(b.getDescription());
System.out.println("总共是:" + b.cost());
}
}}下面是输出的结果
請選擇:
1--> Empresso
2--> HousBlend
-----------------------
1
-----------------------
請選擇調料:
1--> Mocha
2--> Milk
3--> Don't Anything
-----------------------
1
-----------------------
是否继续选择调料
1--> yes
2--> no
-----------------------
2
你好!你购买的coffee是--:
coffer: Espresso1.9
Mocha: 2.0总共是:3.9
你好!你购买的coffee是:
coffer: Espresso1.9
Mocha: 2.0总共是:3.9
-----------------------
對不起!沒有你選擇的coffee
請選擇:
1--> Empresso
2--> HousBlend
-----------------------为什么本来只想结算一次,结果出来了还几次的结算 ,把上面3个方法的else都输出了。
看下要怎么改
public class StarBuzzCoffee {
Scanner scan=new Scanner(System.in);
public static void main(String[] args){
Beverage b = null;
StarBuzzCoffee sc=new StarBuzzCoffee();
sc.chooseCoffee(b);
}
//选择咖啡
public void chooseCoffee(Beverage b){
System.out.println("請選擇:");
System.out.println("1--> Empresso");
System.out.println("2--> HousBlend");
System.out.println("-----------------------");
int i=scan.nextInt();
if(i==1){
b=new Espresso();
chooseCondiment(b);
}
if(i==2){
b=new HousBlend();
chooseCondiment(b);
}
else{
System.out.println("-----------------------");
System.out.println("對不起!沒有你選擇的coffee");
chooseCoffee(b);
}
}
//调料选择 public void chooseCondiment(Beverage b){
System.out.println("-----------------------");
System.out.println("請選擇調料:");
System.out.println("1--> Mocha");
System.out.println("2--> Milk");
System.out.println("3--> Don't Anything");
System.out.println("-----------------------");
// Scanner scan=new Scanner(System.in);
int i=scan.nextInt();
if(i==1){
b=new Mocha(b);
accounts(b);
}
if(i==2){
b=new Milk(b);
accounts(b);
}
else{
System.out.println("你好!你购买的coffee是:");
System.out.println(b.getDescription());
System.out.println("总共是:" + b.cost());
}
}
//结账
public void accounts(Beverage b){
System.out.println("-----------------------");
System.out.println("是否继续选择调料");
System.out.println("1--> yes");
System.out.println("2--> no");
System.out.println("-----------------------");
// Scanner scan=new Scanner(System.in);
int i=scan.nextInt();
if(i==1){
chooseCondiment(b);
}
else{
System.out.println("你好!你购买的coffee是--:");
System.out.println(b.getDescription());
System.out.println("总共是:" + b.cost());
}
}}下面是输出的结果
請選擇:
1--> Empresso
2--> HousBlend
-----------------------
1
-----------------------
請選擇調料:
1--> Mocha
2--> Milk
3--> Don't Anything
-----------------------
1
-----------------------
是否继续选择调料
1--> yes
2--> no
-----------------------
2
你好!你购买的coffee是--:
coffer: Espresso1.9
Mocha: 2.0总共是:3.9
你好!你购买的coffee是:
coffer: Espresso1.9
Mocha: 2.0总共是:3.9
-----------------------
對不起!沒有你選擇的coffee
請選擇:
1--> Empresso
2--> HousBlend
-----------------------为什么本来只想结算一次,结果出来了还几次的结算 ,把上面3个方法的else都输出了。
看下要怎么改
解决方案 »
- 关于java date与win32 filetime格式之间的转换
- 使用JB9中的JTable,遇到N多困惑·
- Java界面制作的几个简单问题。
- 如何实现在同一台电脑上有两个jdk版本如1.4与1.5各一个呀
- 实现和 jdk\bin\native2ascii.exe 同样的功能并能反向转换?
- 又一个String,如何使字符和字符之间增加一个*号
- int类数据 除法计算问题
- 毕业设计中的一部分,请帮忙!!
- 有没有大虾能给介绍一下java啊~!?让新手也能对java有一个概念~!决定自己的发展方向啊?
- 请问有誰开发过分布式系统,请谈谈三层结构,特别是中间层的实现,急!!!!
- super.init(config)问 题急急急
- ResultSet 如何得到记录总数
b=new Mocha(b);
accounts(b);
}else
if(i==2){
b=new Milk(b);
accounts(b);
}
else{
System.out.println("你好!你购买的coffee是:");
System.out.println(b.getDescription());
System.out.println("总共是:" + b.cost());
}试一下