【问题标题】:How do i make the program keep looping until the user has entered an integer我如何让程序一直循环,直到用户输入一个整数
【发布时间】:2015-02-01 01:40:37
【问题描述】:

我正在尝试修改我的程序,以便即使用户输入了字符串而不是程序崩溃,它也应该继续循环并要求用户输入需要为整数的考试成绩,只有当用户如果程序终止,则输入一个整数。我指的是 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();

}

  }

【问题讨论】:

    标签: java do-while


    【解决方案1】:

    使用布尔变量来跟踪是否继续循环。例如:

    boolean loop = true;
    do {
        // set loop to false when you want to end
    } while(loop);
    

    【讨论】:

      【解决方案2】:

      所以你可以这样做:

      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
      

      【讨论】:

        【解决方案3】:

        您可以将其输入为String 并循环直到该字符串代表int,而不是假设输入的数字是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
            }
        }
        

        【讨论】:

          【解决方案4】:

          您是否考虑过使用 JOptionPane 来获取用户的输入?它能够显示一个带有文本字段和一个确定和取消按钮的小窗口,非常适合您的需求。

          这里是 JOptionPane#showInputDialog 的文档:

          static String showInputDialog(Component parentComponent, Object message, String title, int messageType) 
          

          显示一个对话框,请求来自 parentComponent 的用户的输入,该对话框的标题为标题,消息类型为 messageType。

          【讨论】:

            猜你喜欢
            • 2018-07-15
            • 2020-08-15
            • 2021-09-09
            • 2021-07-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多