提问人:Sotei 提问时间:9/23/2022 更新时间:9/23/2022 访问量:185
如何设置在输入特定字符串输入时结束的循环条件
How to set up a loop condition that ends when a particular string input is entered
问:
这是我问题的输入部分。 编写一个程序,该程序将持续允许用户输入以下内容:
*3 种不同情况下的温度
3 个输入(摄氏度、华氏度、开尔文)的温度单位
换算温度单位(摄氏度、华氏度、开尔文)
如果这些条件都不满足或输出已打印,则它将返回询问
用户输入新值。
如果在输入阶段的任何时候,用户输入“退出”一词,则结束程序。*
我能够完成问题中的所有操作,但我跳过了上面的最后一个条件,因为我不知道如何设置它。 现在这是我程序的输入阶段:
Scanner sc = new Scanner(System.in);
System.out.println("Please give three numbers");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int num3 = sc.nextInt();
System.out.println("Choose the temperature unit of the three numbers.");
System.out.println("Enter 1 for Celsius, 2 for Fahrenheit and 3 for Kelvin");
int tempUnit = sc.nextInt();
switch (tempUnit) {
case (1) -> System.out.println("Celsius was chosen.");
case (2) -> System.out.println("Fahrenheit was chosen.");
case (3) -> System.out.println("Kelvin was chosen.");
}
System.out.println("Choose the temperature unit you want to convert it into.");
System.out.println("Enter 1 for Celsius, 2 for Fahrenheit and 3 for Kelvin");
int chosenTemp = sc.nextInt();
switch (chosenTemp) {
case (1) -> System.out.println("Celsius was chosen.");
case (2) -> System.out.println("Fahrenheit was chosen.");
case (3) -> System.out.println("Kelvin was chosen.");
}
只要在任何时候输入字符串“Quit”,循环结束程序就会结束。
顶部的 while 循环将完成,其中包含检查输入是否输入“Quit”的条件
所有东西,但我大部分时间都失败了,因为如果不同的数据类型是
输入比被问到的要多。
有人告诉我,我的提示没有设置为接收我的 while 循环的“退出”。任何人都可以
给我举个例子或教我如何设置?
答:
0赞
TerZer
9/23/2022
#1
首先,我们创建一些用于读取用户输入的辅助方法:
public static int readNumber(){
Scanner sc = new Scanner(System.in);
String string = sc.nextLine(); //Read anything that user typed
//Terminate program if "quit" was typed
if(string.equalsIgnoreCase("quit")){
System.exit(0);
}
try {
return Integer.parseInt(string);
} catch (Exception e){
return readNumber(); //If user typed gibberish - retry
}
}
public static int readNumber(int lowerBound, int upperBound){
int number = readNumber();
if(number < lowerBound || number > upperBound){
return readNumber(lowerBound, upperBound);
}
return number;
}
在那之后,更改主程序:
public static void main(String[] args) {
//Run program indefinitely
while(true) {
System.out.println("Please give three numbers");
int num1 = readNumber();
int num2 = readNumber();
int num3 = readNumber();
System.out.println("Choose the temperature unit of the three numbers.");
System.out.println("Enter 1 for Celsius, 2 for Fahrenheit and 3 for Kelvin");
int tempUnit = readNumber(1, 3);
switch (tempUnit) {
case 1: System.out.println("Celsius was chosen.");
case 2: System.out.println("Fahrenheit was chosen.");
case 3: System.out.println("Kelvin was chosen.");
}
System.out.println("Choose the temperature unit you want to convert it into.");
System.out.println("Enter 1 for Celsius, 2 for Fahrenheit and 3 for Kelvin");
int chosenTemp = readNumber(1, 3);
switch (chosenTemp) {
case 1: System.out.println("Celsius was chosen.");
case 2: System.out.println("Fahrenheit was chosen.");
case 3: System.out.println("Kelvin was chosen.");
}
}
}
0赞
vsfDawg
9/23/2022
#2
将用于从用户获取输入的逻辑移动到单独的方法中。即使在小程序中,这通常也是一个好主意,因为它允许您一次专注于一件事。
private int readInt();
静态定义扫描仪或将其作为输入提供。为简单起见,我要说的是,我们将它作为输入提供,因此我们将将其添加为参数。
private int readInt(Scanner scanner);
该方法将从用户那里读取一行输入,如果程序与“quit”匹配,则终止程序,然后解析并返回整数值。它将报告错误的输入,并允许用户重试,直到他们退出或输入有效的 int 值。
private int readInt(Scanner scanner) {
while (true) {
String line = scanner.readLine();
if ("quit".equalsIgnoreCase(line)) {
System.exit(0);
}
try {
return Integer.parse(line);
} catch (Exception e) {
System.out.println("That does not appear to be an integer - try again");
}
}
}
用法将如下所示
Scanner scanner = new Scanner(System.in);
... stuff ...
int value = readInt(scanner);
评论
do{...}while(!sc.nextLine().equalsIgnoreCase("quit"));