java IF NOT语句

时间:2012-09-27 17:44:37

标签: java

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);


    }
}

如何向这段代码添加一条语句,如果不是NUMERIC INPUT System.out.println(“输入无效,请仅使用类型编号!”)

7 个答案:

答案 0 :(得分:6)

在调用Scanner之前,您应该使用hasNextInt()的{​​{1}}方法确定输入是否为数字:

nextInt

答案 1 :(得分:3)

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

所以你应该考虑到这一点将代码包装在try-catch中

答案 2 :(得分:1)

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

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

     <your if statements>

  } catch(Exception ex)  
  {  
    System.out.println("Your comment");
  }  

答案 3 :(得分: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);

答案 4 :(得分:0)

使用扫描仪时,您永远不会知道它是否是已输入的整数。它将等到输入“nextInt”。你可以做的是使用

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

猜猜一个字符串并使用。 guess = keyboard.next();

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

答案 5 :(得分: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);


    }
}

答案 6 :(得分: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);


    }
}