请求整数作为输入,如果q则退出,但如果有任何其他字母再试一次

时间:2017-12-03 05:01:11

标签: java java.util.scanner

我正在开发一个程序,它会询问用户一个整数然后另一个整数,并将它们作为图形点。然后它会告诉他们它们之间的距离。用户必须输入一个整数或按" Q"退出。如果它是其他任何东西(不是整数或字母" Q"),它会告诉他们这是不正确的,请再试一次。我认为这是我可以做到的,但它会返回错误cannot find symbol。非常感谢帮助!

import java.util.Scanner;

public class PointCheck{

   public static void main(String[] args){

      try {

         System.out.println("Welcome");
         System.out.println("To quit at anytime please type \"Q\". Enter point:");

         char Q = 'Q';
         char q = 'q';
         Scanner scan = new Scanner (System.in);
          input = scan.next();

         while (input.hasNext()) {
            if (input.hasNextInt()) {
               System.out.println("Int input");
            }
            else if (input.equals("Q")) {
               System.out.println("Exiting");

            }
            /*else {
               System.out.println("You did not enter a valid value. Please enter a number or \"Q\" to quit.");
            }*/

         }

      }

      catch(Exception e){
         System.out.println("Exiting Program.");

      }

   }

}

如果我没有注释掉最后一个else语句,那么它就会默认为那个并永远循环我的错误信息。

3 个答案:

答案 0 :(得分:1)

我对你的代码进行了一些修改,希望它有所帮助:)

public class PointCheck {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    try {

        System.out.println("Welcome");
        System.out.println("To quit at anytime please type \"Q\". Enter point:");
        Scanner scan = new Scanner(System.in);

        while (scan.hasNext()) {
            if (scan.hasNextInt()) {
                System.out.println("Int input" + scan.nextInt());
            } else {
                String input = scan.next();
                if (input.equalsIgnoreCase("Q")){
                    System.out.println("Exiting");
                    break;

                }else {
                    System.out.println("You did not enter a valid value. Please enter a number or \"Q\" to quit.");
                }
            }

        }

    } catch (Exception e) {
        System.out.println("Exiting Program.");

    }
}

}

答案 1 :(得分:0)

你可以尝试这个...... 输入将只使用整数,如果您输入Integer以外的任何内容,它将抛出异常并移至程序将终止的catch块。

import java.util.Scanner;

public class demo {
    public static void main(String[] args) {
        System.out.println("Welcome");
        System.out.println("Press any key to exit.. Enter point:");
        Scanner scanner= new Scanner(System.in);

        while (true){
            try {
                int number = scanner.nextInt();
                System.out.println(number);
            }
            catch (Exception e){
                System.exit(0);
            }
        }
    }
}

答案 2 :(得分:0)

这样的事可能是你喜欢的。阅读代码中的注释:

System.out.println("Welcome");
Scanner scanner= new Scanner(System.in);
int number = 0;
while (true){
    System.out.println("Enter point (q to quit): ");
    String strg = scanner.nextLine();
    // Get out of loop if q or Q or quit, 
    // or QUIT, or Quit, etc is entered.
    // providing the input actually contains
    // something.
    if (!strg.equals("") && strg.toLowerCase().charAt(0) == 'q') {
        break;
    }
    // Use the String.matches() method with regex to 
    // determine if an integer number was supplied.
    if (!strg.matches("\\d+")) {
        System.err.println("Invalid Entry - Integer values only! Try again.\n");
        continue;   // Start loop from beginning.
    }
    // Convert string number to Integer.
    number = Integer.parseInt(strg);
    break;  // Get outta loop      
}
String msg = "The number entered is: " + number;
if (number == 0) { msg = "Nothing Entered!"; }
System.out.println(msg);