大家看下这个简单的程序为什么不能输出理想的结果!
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都输出了。
看下要怎么改
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());
}试一下