gpt4 book ai didi

Java:两个输入具有相同的初始字符

转载 作者:太空宇宙 更新时间:2023-11-04 11:36:34 25 4
gpt4 key购买 nike

我遇到一个问题,披萨的配料之一“mozarella”也具有与中等的“m”相同的初始字符。我怎样才能做到这一点,以便如果用户在第一个字符后输入 "m" ,它会将 "m" 识别为莫扎里拉奶酪,而不是中号奶酪。

目前,如果我输入mm,它会打印您的订单是:中号披萨,中号披萨,£8.0,但我希望它打印您的订单是:中号披萨,马苏里拉奶酪,£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初学者,所以我仍在学习,任何帮助将不胜感激。

最佳答案

首先,一个简单的更改是将媒体的代码从 m 更改为 md。但是您可以检查尺寸是否已添加(因此用户已经添加了 m 或 l),如果是这样,那么另一个 m 将是马苏里拉奶酪。您可以通过在代码中添加一个计数器来计算您是否已读取第一个字符,因此如果

计数器>0

m 将是马苏里拉奶酪。像这样应该没问题

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 ));
}

}

关于Java:两个输入具有相同的初始字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43211048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com