[英]How do i make the program keep looping until the user has entered an integer
i am trying to modify my program so that even when the user has entered a string instead of the program crashing it should keep looping and asking for the user to enter the exam grade which needs to be an integer, only when the user has entered an integer should the program terminate. 我正在尝试修改程序,以便即使用户输入了字符串而不是程序崩溃,它也应继续循环并要求用户输入必须为整数的考试成绩,仅当用户输入了整数,程序应终止。 I am referring to the code in the do-while block 我指的是do-while块中的代码
import java.util.InputMismatchException;
import java.util.Scanner;
public class CatchingException {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int score;
String choice;
try {
System.out.println("Enter your percentage mark: ");
score = scan.nextInt();
do {
if(score <40) {
System.out.println("You FAILED");
}else if(score >=40 && score <50){
System.out.println("Your grade: PASS MARK");
}else if(score >=50 && score <60) {
System.out.println("Your grade: 2:2");
}else if (score >=60 && score <70) {
System.out.println("Your grade: 2:1");
}else {
System.out.println("Your grade: 1:1");
}
System.out.println("Do you want to enter another grade: ");
choice = scan.next();
if(choice.equalsIgnoreCase("yes")) {
System.out.println("Enter your percentage mark: ");
score = scan.nextInt();
System.err.println("Incorrect Input");
}
}while();
}catch(InputMismatchException e) {
System.err.println("Incorrect Input ");
}
System.out.println("program terminated");
scan.close();
}
}
Use a boolean variable to keep track of whether or not to keep looping. 使用布尔变量来跟踪是否继续循环。 For example: 例如:
boolean loop = true;
do {
// set loop to false when you want to end
} while(loop);
so you could do: 所以你可以这样做:
int score = null;
boolean isInt = false;
do {
System.out.println("Enter your percentage mark:");
try {
score = scan.nextInt();
isInt = true;
} catch (InputMismatchException e) {
//Not An Integer
isInt = false;
}
} while(false)
//Do you if statements here if it gets out of the while loop which means the user entered an int
Instead of assuming the number inputed is an int
, you can input it as a String
and loop until that string represents an int
: 相反的假设inputed数为int
,你可以输入它作为一个String
和循环,直到该字符串表示int
:
int intScore;
String score;
boolean gotInt = false;
while (!gotInt) {
score = scan.next();
try {
intScore = Integer.valueOf(score);
gotInt = true;
} catch (NumberFormatException e) {
// output some warning
}
}
Did you consider using JOptionPane to get an input from the user? 您是否考虑过使用JOptionPane从用户那里获取输入? It is able to display a little window with a text field and a OK and Cancel button which would fit your needs perfectly. 它能够显示一个带有文本字段以及“确定”和“取消”按钮的小窗口,非常适合您的需求。
Here is the documentation for JOptionPane#showInputDialog: 这是JOptionPane#showInputDialog的文档:
static String showInputDialog(Component parentComponent, Object message, String title, int messageType)
Shows a dialog requesting input from the user parented to parentComponent with the dialog having the title title and message type messageType. 显示一个对话框,该对话框请求以parentComponent为父级的用户输入,该对话框具有标题标题和消息类型messageType。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.