首先,我想当他们给一个错误的输入,请那错误的输入写在那里的逻辑输入。
当我按下空格时,每一个空格都会重复替换。
喜欢
请输入送货速度。
如果要查看所有的交付速度选项,请输入“选项”。
24小时
输入错误,请阅读选项,并注意大写字母。
请输入送货速度。
如果要查看所有的交付速度选项,请输入“选项”。
输入错误,请阅读选项,并注意大写字母。
请输入送货速度。
如果要查看所有的交付速度选项,请输入“选项”。
123 123 123
输入错误,请阅读选项,并注意大写字母。
请输入送货速度。
如果要查看所有的交付速度选项,请输入“选项”。
输入错误,请阅读选项,并注意大写字母。
请输入送货速度。
如果要查看所有的交付速度选项,请输入“选项”。
输入错误,请阅读选项,并注意大写字母。
请输入送货速度。
如果要查看所有的交付速度选项,请输入“选项”。
这是我的密码。
请帮帮我。
import java.util.InputMismatchException;
import java.util.Scanner;
public class deneme123 {
public static void main(String[] args){
String opt=null;
Scanner input=new Scanner(System.in);
double cost= 0.0;
while (true) {
while (true) {
System.out.println("Please enter a delivery speed.\nIf you want to see all options of delivery speed, enter 'options'.");
try {
opt = input.next();
break;
}
catch (InputMismatchException e) {
System.out.println("Wrong type, pls try again!");
input.nextLine();
}
}
if (opt.equals("fast")) {
cost = cost + 34.9;
System.out.println("Total is updated to " + cost + "$");
break;
}
else if (opt.equals("average")) {
cost = cost + 17.5;
System.out.println("Total is updated to " + cost + "$");
break;
}
else if (opt.equals("standart")) {
cost = cost + 5.5;
System.out.println("Total is updated to " + cost + "$");
break;
}
else if (opt.equals("VIP")||opt.equals("vip")||opt.equals("Vip")) {
cost = cost + 50;
System.out.println("Total is updated to " + cost + "$");
break;
}
else if (opt.equals("options")) {
System.out.println("* For VIP delivery speed enter 'VIP'. (costs 50 $) " +
"\n* For fast delivery speed enter 'fast'. (costs 34.9 $) " +
"\n* For average delivery speed enter 'average' (costs 17.5 $)" +
"\n* For standart delivey speed enter 'standart' (costs 5.5 $)");
}
else {
System.out.println("Wrong type, please read options and be careful with the capital letters.");
}
}
}
}发布于 2016-11-04 22:54:04
我稍微修改了你的代码。在代码下面,我展示了运行它时得到的输出。
package deneme23;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
*
* @author davec
*/
public class Deneme23 {
public static void main(String[] args){
String [] opts=null;
Scanner input=new Scanner(System.in);
double cost= 0.0;
OUTER:
while (true) {
while (true) {
System.out.println("Please enter a delivery speed.\nIf you want to see all options of delivery speed, enter 'options'.");
try {
String line = input.nextLine();
opts = line.split("\\s");
break;
}
catch (InputMismatchException e) {
System.out.println("Wrong type, pls try again!");
input.nextLine();
}
}switch (opts[0]) {
case "fast":
cost = cost + 34.9;
System.out.println("Total is updated to " + cost + "$");
break OUTER;
case "average":
cost = cost + 17.5;
System.out.println("Total is updated to " + cost + "$");
break OUTER;
case "standart":
cost = cost + 5.5;
System.out.println("Total is updated to " + cost + "$");
break OUTER;
case "VIP":
case "vip":
case "Vip":
cost = cost + 50;
System.out.println("Total is updated to " + cost + "$");
break OUTER;
case "options":
System.out.println("* For VIP delivery speed enter 'VIP'. (costs 50 $) " +
"\n* For fast delivery speed enter 'fast'. (costs 34.9 $) " +
"\n* For average delivery speed enter 'average' (costs 17.5 $)" +
"\n* For standart delivey speed enter 'standart' (costs 5.5 $)");
break;
default:
System.out.println("Wrong type, please read options and be careful with the capital letters.");
break;
}
}
}
} 这是我运行它时得到的输出:
Please enter a delivery speed.
If you want to see all options of delivery speed, enter 'options'.
24 hours
Wrong type, please read options and be careful with the capital letters.
Please enter a delivery speed.
If you want to see all options of delivery speed, enter 'options'.
options
* For VIP delivery speed enter 'VIP'. (costs 50 $)
* For fast delivery speed enter 'fast'. (costs 34.9 $)
* For average delivery speed enter 'average' (costs 17.5 $)
* For standart delivey speed enter 'standart' (costs 5.5 $)
Please enter a delivery speed.
If you want to see all options of delivery speed, enter 'options'.
fast
Total is updated to 34.9$https://stackoverflow.com/questions/40432311
复制相似问题