简体   繁体   English

我如何使程序保持循环,直到用户输入了整数

[英]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.

相关问题 如何获得此代码块以保持循环,直到输入正确的学生编号? - How do I get this block of code to keep looping until a correct student number is entered? 如何让程序一直询问直到输入有效数字? - how to make the program to keep asking until the a valid number is entered? 循环用户输入,直到输入正的非零整数 - Looping user input until a positive nonzero integer is entered 我如何让我的Java程序等到在文本字段中输入值 - How do i make my java program wait until there is entered a value into the textfield 如何循环用户输入直到输入 integer? - How to loop a user input until integer is entered? 如何让我的程序显示用户输入的整数的相反顺序? - How do I make my program show the reverse order of the integers that was entered by the user? 如何让程序检查错误,然后继续循环 - How do I have a program check for errors and then keep looping 如何继续循环直到用户输入有效长度的字符串? - How to keep looping through until user has inputted string of valid length? 如何使具有多个类的程序运行,直到用户指定“ E”而不是在Main的末尾终止? - How do I make a program with multiple classes run until the user specifies “E” instead of terminating at the end of Main? Java-如何在输入特定值之前运行程序 - Java - How to make a program run until a specific value is entered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM