[英]If the user enters an invalid code how to code it?
If the user enters an invalid code, display You selected an invalid option and set the price to 0. 如果用户输入了无效的代码,则显示“您选择了无效的选项,并将价格设置为0”。
import java.util.Scanner;
public class ShadyRestRoom {
public static void main(String[]args) {
Scanner scanner= new Scanner(System.in);
int QueenBed = 125;
int KingBed = 139;
int KingPullout = 165;
System.out.println("Choose: 1 for a Queen bed Choose: 2 for a King bed or Choose: 3 for a King bed with a pullout couch");
int RoomChoice= scanner.nextInt();
if (RoomChoice == 1)
System.out.println("$125 for a Queen bed");
if (RoomChoice == 2)
System.out.println("$139 for a King bed");
if (RoomChoice == 3)
System.out.println("$165 for a King bed with a pullout couch");
while (RoomChoice == 4)
System.out.println("ERROR Please enter a valid Choice");
RoomChoice = scanner.nextInt();
if (RoomChoice == 8)
System.out.println("ERROR Please enter a valid Choice");
RoomChoice = scanner.nextInt();
}
}
I can't seem to figure out how to set it so it does= ( If the user enters an invalid code, display You selected an invalid option and set the price to 0.) 我似乎无法弄清楚如何设置它,所以它=(如果用户输入无效的代码,则显示“您选择了无效的选项并将价格设置为0。”)
One way do accomplish a choice is a switch statement. 完成选择的一种方法是switch语句。 It is made for it. 它是为此做的。 So a choice calls for a switch. 因此,选择需要进行切换。
Now in a switch you have for every valid choice one "case" statement. 现在,在开关中,对于每个有效选择,您都有一个“ case”语句。 Additionally in the end there is a "default" for everything else not mentioned explicitely. 此外,最后还有一个“默认值”,用于未明确提及的所有其他内容。
You can use that default to "cheat" and reset a variable to a defined value, of your choice. 您可以使用默认值“ cheat”,并将变量重置为定义的值。
Now that again makes the condition in the loop simpler as it isn't so vague("some input except..") anymore but there is only one defined value that says "another loop". 现在,这又使循环中的条件变得更简单了,因为它不再那么模糊(“除了..以外还有其他输入”),但是只有一个定义的值表示“另一个循环”。
I took "-1" as an indicator, that says "something went wrong". 我以“ -1”作为指标,表示“出了点问题”。 But you can use whatever makes sence to you as "escape"(thats what such values are called: -1 is an "escape code"). 但是您可以将任何有意义的信息用作“转义符”(这就是所谓的值:-1是“转义码”)。 And so the while checks for my escape "-1" and continues asking until the user gives something in range. 因此,while会检查我的转义符“ -1”,并继续询问,直到用户给出范围内的内容为止。
public static class ShadyRestRoom {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int QueenBed = 125;
int KingBed = 139;
int KingPullout = 165;
int RoomChoice = -1;
do {
System.out.println("Choose: 1 for a Queen bed Choose: 2 for a King bed or Choose: 3 for a King bed with a pullout couch");
RoomChoice = scanner.nextInt();
switch (RoomChoice) {
case 1:
System.out.println("$125 for a Queen bed");
break;
case 2:
System.out.println("$139 for a King bed");
break;
case 3:
System.out.println("$165 for a King bed with a pullout couch");
break;
//you can also explicitely take a group of numbers that need to be treated the same way:
case 4:
case 8:
default: //this is the default for everything that wasn't metioned before
RoomChoice = -1;
System.out.println("ERROR Please enter a valid Choice");
}
} while (-1 == RoomChoice);
}
}
Btw: it is a convention to use lower case letters for variables. 顺便说一句:习惯上将小写字母用于变量。 So "roomChoice" instead of "RoomChoice" would be better. 因此,以“ roomChoice”代替“ RoomChoice”会更好。 With that you could see immediately that is a variable and not class. 这样,您可以立即看到这是一个变量而不是类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.