如何在有三种选择限制的自动售货机中使用for循环



我不认为这是正确的,我该如何使用for循环?我需要使用all while和for循环以及if语句。

Scanner sc=新扫描仪(System.in(;

final double POTATO_CHIPS_PRICE = 1.25;
final double SNICKERS_PRICE = 0.80;
final double KIND_BAR_PRICE = 0.70;
int snackSelection = 0;
// final int POTATO_CHIPS = A1,A2,A3;
System.out.println("*******************************************");
System.out.println("              Snack Machine                ");
System.out.println("*******************************************");
System.out.println("Potato Chips   Potato Chips  Potato Chips");
System.out.println("A1  $1.25      A2  $1.25     A3  $1.25   ");
System.out.println("--------------------------------------------");
System.out.println("Snickers       Snickers      Snickers ");
System.out.println("B1  $0.80      B2  $0.80     B3  $0.80");
System.out.println("--------------------------------------------");
System.out.println("Kind Bar       Kind Bar      Kind Bar");
System.out.println("C1  $0.70      C2  $0.70     C3  $0.70");
System.out.println("--------------------------------------------");
System.out.println("How many snacks would you like?  Limit is 3:");
snackSelection = sc.nextInt();
for (double snack <= 1; snack < 3; snack++) {
System.out.println("Enter snack selection:");

}

我假设您希望用户选择三种类型的零食。如果用户必须选择特定数量的零食,你可以使用for循环,但在大多数情况下whileloop会更好,因为你可以更好地控制循环何时结束。

例如,如果用户没有键入正确类型的零食,For循环仍然会迭代,结果用户可能会得到少于三个零食。

使用for循环类似于

int amount = 0;
for (int i = 0; i < 3; i++) {
println("What snack would you like?");
String selection = sc.nextLine();
if (!selection.equals("A1") || !!selection.equals("B2") ||!!selection.equals("C3")) {
println("Undefined snack, please select A1, B1 or C1")
} else {
if(selection.equals("A1") {
amount += POTATO_CHIPS_PRICE;
} else if (selection.equals("B2") {
amount += SNICKERS_PRICE;
} else {
amount += KIND_BAR_PRICE;
}
}
}

您需要添加对其他金额/价格的支持,但这是乐趣的一部分!这也是Java而不是JavaScript!容易混淆。

static final List<String> VALID_SNACKS = Arrays.asList("A1", "B1", "C1");
public static void main(String[] args) {
System.out.println("*******************************************");
System.out.println("              Snack Machine                ");
System.out.println("*******************************************");
System.out.println("Potato Chips   Potato Chips  Potato Chips");
System.out.println("A1  $1.25      A2  $1.25     A3  $1.25   ");
System.out.println("--------------------------------------------");
System.out.println("Snickers       Snickers      Snickers ");
System.out.println("B1  $0.80      B2  $0.80     B3  $0.80");
System.out.println("--------------------------------------------");
System.out.println("Kind Bar       Kind Bar      Kind Bar");
System.out.println("C1  $0.70      C2  $0.70     C3  $0.70");
System.out.println("--------------------------------------------");
Scanner scanner = new Scanner(System.in);
int amount = -1;
while (amount == -1) {
System.out.println("How many snacks would you like?  Limit is 3:");
int input = scanner.nextInt();
if (input < 1 || input > 3) {
System.out.println("You can only have 1 to 3 stacks.");
continue;
}
amount = input;
}
String type = null;
while (type == null) {
System.out.println(String.format("What type of snack do you want %s of?", amount));
String input = scanner.next();
int indexOfType = VALID_SNACKS.indexOf(input);
if (indexOfType == -1) {
System.out.println("That is not a valid snack.");
continue;
}
type = input;
}
System.out.println(String.format("Okay! %sx %s coming right up!", amount, type));
}

最新更新