scanner的问题

pingqunnayang 2009-10-30 01:27:45
大家看下这个简单的程序为什么不能输出理想的结果!


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都输出了。
看下要怎么改

...全文
43 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
pingqunnayang 2009-10-30
  • 打赏
  • 举报
回复
Ok 了
害我想了那么久
loveofmylife 2009-10-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 pingqunnayang 的回复:]
没看懂楼上的修改方法
[/Quote]
丫,就是第二个if前面多了一个else
pingqunnayang 2009-10-30
  • 打赏
  • 举报
回复
没看懂楼上的修改方法
loveofmylife 2009-10-30
  • 打赏
  • 举报
回复
if(i==1){
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());
}

试一下

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧