我遇到一个问题,其中披萨"mozarella"
的其中一个浇头也与"m"
的初始字符相同。
我怎样才能做到这一点,如果用户在第一个字符后输入"m"
,它会将"m"
识别为mozarella而不是中等。
目前,如果我输入mm
,则会打印Your order is: Medium pizza with, Medium pizza with, £8.0
,但我希望它能够打印Your order is: Medium pizza with,mozarella,£5.0
。
public class pizza {
public static void main(String[] args){
pizzaServiceA();
}
public static void pizzaServiceA(){
Scanner input = new Scanner(System.in);
double total = 0;
System.out.println("Enter order:");
String order = input.next();
String pizza = "Your order is: ";
if (order.equals("quit")){
System.out.println("Program exiting.");
System.exit(0);
}
boolean size = false;
for (int x = 0; x < order.length(); x++){
if (order.charAt(0) == 'm' || order.charAt(0) == 'l'){
if (order.charAt(x) == 'm'){
total +=4.00;
pizza += "Medium pizza with,";
size = true;
}
else if (order.charAt(x) == 'l'){
total +=5.00;
pizza +="Large pizza with,";
}
else if (order.charAt(x) == 'h'){
pizza +="ham,";
if (size) total += 1.40;
else total +=2.10;
}
else if (order.charAt(x)== 'o'){
pizza +="olives,";
if(size) total +=0.80;
else total +=1.20;
}
else if (order.charAt(x)=='p'){
pizza+="pineapple,";
if(size) total +=1.00;
else total+=1.50;
}
else if (order.charAt(x)=='s'){
pizza+="spinach,";
if(size) total +=0.80;
else total+=1.20;
}
else if (order.charAt(x)=='m'){
pizza +="mozarella,";
if(size) total =(long) + 1.00;
else total+=1.50;
}
}
else {
System.out.println("Your first character must be m(medium) or l(large)");
}
}
System.out.println(pizza +"£" + String.format("%.2f", total ));
}
}
如果我需要为此程序使用不同的方法,有人可以解释我需要使用什么以及如何使用它?我是java的初学者,所以我还在学习,任何帮助都会受到赞赏。
答案 0 :(得分:0)
首先,一个简单的改变是将介质的代码从m更改为md。但是你可以检查尺寸是否已经添加(因此用户已经添加了m或l),如果这是真的,那么另一个m将是莫扎里拉奶酪。您可以通过在代码中添加一个计数器来计算是否已经读过第一个字符,所以如果
counter>0
public class pizza {
public static void main(String[] args){
pizzaServiceA();
}
public static void pizzaServiceA(){
Scanner input = new Scanner(System.in);
double total = 0;
System.out.println("Enter order:");
String order = input.next();
String pizza = "Your order is: ";
if (order.equals("quit")){
System.out.println("Program exiting.");
System.exit(0);
}
int count=0;
boolean size = false;
for (int x = 0; x < order.length(); x++){
if (order.charAt(0) == 'm' || order.charAt(0) == 'l'){
if (order.charAt(x) == 'm' && count ==0){
total +=4.00;
pizza += "Medium pizza with,";
size = true;
}
else if (order.charAt(x) == 'm' && count > 0){
pizza +="mozarella,";
if(size) total =(long) + 1.00;
else total+=1.50;
}
else if (order.charAt(x) == 'l'){
total +=5.00;
pizza +="Large pizza with,";
}
else if (order.charAt(x) == 'h'){
pizza +="ham,";
if (size) total += 1.40;
else total +=2.10;
}
else if (order.charAt(x)== 'o'){
pizza +="olives,";
if(size) total +=0.80;
else total +=1.20;
}
else if (order.charAt(x)=='p'){
pizza+="pineapple,";
if(size) total +=1.00;
else total+=1.50;
}
else if (order.charAt(x)=='s'){
pizza+="spinach,";
if(size) total +=0.80;
else total+=1.20;
}
}
else {
System.out.println("Your first character must be m(medium) or l(large)");
}
count++;
}
System.out.println(pizza +"£" + String.format("%.2f", total ));
}
}
答案 1 :(得分:0)
尝试区分大小写。记住mozzarella是“m”还是“M”(仅当你选择将medium
改为“m”而mozzarella
改为“M”,反之亦然时,可能会很烦人,但是也许你可以把它变成“mzz”。同样的原则同样适用于"medium"
;也许可以把它改成像“md”这样的东西,就像@geo建议的那样。