此代码应显示一个菜单,然后它应该让用户可以从菜单中进行选择。用户可以选择3个项目以及数量,然后显示总价格并停止程序。 这是程序在运行时的样子:
This is the menu for Tal'Qroq Restourant:
This is the menu of pizzas
A.Margherita ..... $5.50
B.Capricosa ..... $6.50
C.Funghi ..... $7.00
D.Vegeterian..... $7.00
E.Tropical..... $8.00
F.Meat ..... $8.00
G.Salami..... $8.00
H.Maltija..... $8.00
I.Calzona..... $8.50
J.Tal'Qroq special..... $8.00
Enter your pizza order according to the menu letters:
用户输入比萨饼后,会询问数量并且工作正常。 用户必须被要求3次输入披萨和数量,但循环不会停止并继续询问和无限询问这是我的问题! 以下是代码:
public class Menu{
public static void main (String[]args){
float total = 0;
char cas = 0;
int quant = 0;
int count = 0;
System.out.println("This is the menu for Tal'Qroq Restourant:");
System.out.println("\n");
System.out.println("This is the menu of pizzas");
System.out.println("\n");
System.out.println("A.Margherita ..... $5.50");
System.out.println("\n");
System.out.println("B.Capricosa ..... $6.50");
System.out.println("\n");
System.out.println("C.Funghi ..... $7.00");
System.out.println("\n");
System.out.println("D.Vegeterian..... $7.00");
System.out.println("\n");
System.out.println("E.Tropical..... $8.00");
System.out.println("\n");
System.out.println("F.Meat ..... $8.00");
System.out.println("\n");
System.out.println("G.Salami..... $8.00");
System.out.println("\n");
System.out.println("H.Maltija..... $8.00");
System.out.println("\n");
System.out.println("I.Calzona..... $8.50");
System.out.println("\n");
System.out.println("J.Tal'Qroq special..... $8.00");
System.out.println("\n");
float a = 5.50f;
float b = 6.50f;
float c = 7.00f;
float d = 7.00f;
float e = 8.00f;
float f = 8.00f;
float g = 8.00f;
float h = 8.00f;
float i = 8.00f;
float j = 8.00f;
do{
System.out.print("Enter your pizza order according to the menu letters: ");
cas = Keyboard.readChar();
System.out.print("Enter the ammount of pizza you want: ");
quant = Keyboard.readInt();
if(cas == 'a' || cas == 'A'){
System.out.println("Total for " + quant + " Margherita is :" + (a*quant));
System.out.println("\n");
total = total + (a*quant);
count = count++;
}else if(cas == 'b' || cas == 'B'){
System.out.println("Total for " + quant + " Capricosa is :" + (b*quant));
System.out.println("\n");
total = total + (b*quant);
count = count++;
}else if(cas == 'c' || cas == 'C'){
System.out.println("Total for " + quant + " Funghi is :" + (c*quant));
System.out.println("\n");
total = total + (c*quant);
count = count++;
}else if(cas == 'd' || cas == 'D'){
System.out.println("Total for " + quant + " Vegeterian is :" + (d*quant));
System.out.println("\n");
total = total + (d*quant);
count = count++;
}else if(cas == 'e' || cas == 'E'){
System.out.println("Total for " + quant + " Tropical is :" + (e*quant));
System.out.println("\n");
total = total + (e*quant);
count = count++;
}else if(cas == 'f' || cas == 'F'){
System.out.println("Total for " + quant + " Meat is :" + (f*quant));
System.out.println("\n");
total = total + (f*quant);
count = count++;
}else if(cas == 'g' || cas == 'G'){
System.out.println("Total for " + quant + " Salami is :" + (g*quant));
System.out.println("\n");
total = total + (g*quant);
count = count++;
}else if(cas == 'h' || cas == 'H'){
System.out.println("Total for " + quant + " Calzona is :" + (h*quant));
System.out.println("\n");
total = total + (h*quant);
count = count++;
}else if(cas == 'i' || cas == 'I'){
System.out.println("Total for " + quant + " Maltija is :" + (i*quant));
System.out.println("\n");
total = total + (i*quant);
count = count++;
}else if(cas == 'j' || cas == 'J'){
System.out.println("Total for " + quant + " Tal'Qroq special is :" + (j*quant));
System.out.println("\n");
total = total + (j*quant);
count = count++;
}else{
System.out.println("Your selection isn't avaliable in our Menu!");
System.out.println("\n");
}
} while (count <= 3);
System.out.println("Your total is €" + total);
}
}
任何答案或帮助都非常感谢:)。
答案 0 :(得分:3)
您的陈述ReflectionException in Container.php line 736:
Class swaggable\Http\Controllers\back\controllers\BackToolsController does not exist
看起来不对。仅使用count = count++
或count++
。
使用count = count + 1
创建字节代码有点像这样:
count = count++
因此实际上计数不会增加。
使用 temp = count
count = count + 1
count = temp
应该有效,但逻辑上看错了:
count = ++count
最好不要尝试编译器的有趣的特性。
答案 1 :(得分:0)
int i = 1;
i = i++;
System.out.prinln(i); //-> 1
你必须写&#34; i ++;&#34;然后你很好。
顺便说一下。永远不会在浮动或双重中存钱;)
编辑:
int i = 1;
i = ++i;
这是可能的,因为你在分配新值之前增加了我。
答案 2 :(得分:0)
您可以在程序中改进几件事。首先,让我们来看看这个bug,你说的是count = count ++
但是通过这样做,编译器将首先递增count的值,然后将count恢复为旧的值,因为{{1}将返回不是递增的值。为简化起见,此语句不执行任何操作,也不增加count++
的值。
如果要增加,请使用后缀运算符:
count
;
通过分析您的程序,您可以注意到许多可以减少的重复代码!
例如,在比较输入时,您可以使用count++
,这样您就不必将大小写分割为大写和小写:
.equals()
相当于
(cas == 'f' || cas == 'F')
您可以重复其余的案例。
在每个if语句中,您正在执行相同的cas.equals('f')
。为简单起见,您可以声明一个用于打印的变量,例如
system.out.println
然后在每个String typeofPizza;
if
这样,您可以将打印件移除到typeofPizza = "Margherita";
子句的末尾:
if
答案 3 :(得分:0)
感谢您的早期回复:)。该程序运行良好。我无法相信犯了这样一个愚蠢的错误count ++
而不是++ count
。我还将int count改为 1 而不是<强> 0 强>