2
import java.util.Random;
import java.util.Scanner;

public class Game {
    public static void main(String[] args) {

        System.out.println("Guess a number betwwen 1 and 1000");

        Random rand = new Random(); 
        int secretNumber = rand.nextInt (1000);


        Scanner keyboard = new Scanner(System.in);
        int guess;

        do {
        guess = keyboard.nextInt();

        if (guess == secretNumber)
            System.out.println("You WON!!! Congratulations!");
        else if (guess < secretNumber)
             System.out.println("Nope, to low");
        else if (guess > secretNumber)
            System.out.println("Sorry, to high");


        } while (guess != secretNumber);


    }
}

我如何在此代码中添加一条语句,即 IF NOT NUMERIC INPUT System.out.println("invalid input, please use type numbers only!")

4

7 回答 7

6

您应该在调用之前使用Scanner'hasNextInt()方法来确定输入是否为数字nextInt

do {
    while (!keyboard.hasNextInt()) {
        System.out.println("Please enter only numbers.");
        keyboard.next(); // Skip the wrong token
    }
    // Now that the input is valid, read the value:
    guess = keyboard.nextInt();
    // Put the rest of your logic here
    ...
} while (guess != secretNumber);
于 2012-09-27T17:47:43.417 回答
3

InputMismatchException如果下一个标记与整数正则表达式不匹配或超出范围,则Scanner.nextInt() 抛出

因此,您应该牢记这一点,将代码包装在 try-catch 周围

于 2012-09-27T17:47:55.613 回答
1

我认为您希望以下内容围绕guess = keyboard.nextInt():

 try  
 {  
     guess = keyboard.nextInt()
     Integer.parseInt(guess);  

     <your if statements>

  } catch(Exception ex)  
  {  
    System.out.println("Your comment");
  }  
于 2012-09-27T17:49:11.643 回答
0

您可以在循环中添加一个 try catch 块。

  do {
  try{
    guess = keyboard.nextInt();

    if (guess == secretNumber)
        System.out.println("You WON!!! Congratulations!");
    else if (guess < secretNumber)
         System.out.println("Nope, to low");
    else if (guess > secretNumber)
        System.out.println("Sorry, to high");
    }
catch(InputMismatchException e){
System.out.prinln("Not a number");
}


    } while (guess != secretNumber);
于 2012-09-27T17:50:29.197 回答
0

使用扫描仪时,您永远无法确定它是否是已输入的整数。它只会等到输入“nextInt”。你可以做的是使用

Integer.parseInt() 方法。如果输入字符串不是整数,它将抛出 NumberFormatException。

猜测一个字符串并使用。猜测 = 键盘.next();

然后在 try-catch 中使用 Integer.parseInt(guess) 来解决您的问题。

于 2012-09-27T17:51:05.020 回答
0
public class Game {
    public static void main(String[] args) {

    System.out.println("Guess a number betwwen 1 and 1000");

    Random rand = new Random(); 
    int secretNumber = rand.nextInt (1000);


    Scanner keyboard = new Scanner(System.in);
    int guess;

    do {
    if (!keyboard.hasNextInt()) {
        System.out.println("invalid input, please use type numbers only!");
        return;
    }
    guess = keyboard.nextInt();

    if (guess == secretNumber)
        System.out.println("You WON!!! Congratulations!");
    else if (guess < secretNumber)
         System.out.println("Nope, to low");
    else if (guess > secretNumber)
        System.out.println("Sorry, to high");


    } while (guess != secretNumber);


    }
}
于 2012-09-27T17:54:53.207 回答
0
import java.util.Random;
import java.util.Scanner;

public class Game {
    public static boolean isInteger( String input )  
    {  
       try  
       {  
          Integer.parseInt( input );  
          return true;  
       }  
       catch( Exception e)  
       {  
          return false;  
       }  
    } 

    public static void main(String[] args) {

        System.out.println("Guess a number betwwen 1 and 1000");

        Random rand = new Random(); 
        int secretNumber = rand.nextInt (1000);

        Scanner keyboard = new Scanner(System.in);
        int guess=-1;

        do {
        String g = keyboard.next();
        if(isInteger(g)){
            guess = Integer.parseInt(g);
            if (guess == secretNumber)
                System.out.println("You WON!!! Congratulations!");
            else if (guess < secretNumber)
                System.out.println("Nope, to low");
            else if (guess > secretNumber)
                System.out.println("Sorry, to high");
        }
        else{
            System.out.println("NaN");
        }

        } while (guess != secretNumber);


    }
}
于 2012-09-27T17:55:39.553 回答