如何解决它java.util.InputMismatchException

时间:2015-12-22 06:05:58

标签: exception

这是我的代码: -

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

public class Main {

public static void main(String[] args) {
    Scanner hs = new Scanner(System.in);
    Random ran = new Random();


    int [] player = new int [3];
    int [] com = new int [3];
    //play mode

    System.out.println("Please select mode:  ");
    System.out.println("1. Practice mode.  ");
    System.out.println("2. Game mode.   ");
    System.out.println("3. End.    ");
    int input = hs.nextInt();       //mode
     int draw = 0  ;
     int Player1=0;
     int Com1=0;
    if(input == 1){
        System.out.println("You select Practice mode.");

        for(int i = 0 ; i < 3 ; i++){
            com[i] = ran.nextInt(2);
            if(com[i] == 0){
                System.out.print("剪刀  ");
            }else if(com[i] == 1){
                System.out.print("石頭  ");
            }else if(com[i] == 2){
                System.out.print("布  ");
            }
        }
        for(int i = 0 ; i <= 2 ; i++){
            player[i] = hs.nextInt();
            if(com[i] == 0){
                System.out.print("剪刀  ");
            }else if(com[i] == 1){
                System.out.print("石頭  ");
            }else if(com[i] == 2){
                System.out.print("布  ");
            }



        }
        hs.close();
        for(int i = 0 ; i <=2 ; i++){
            if(player[i]==com[i]){
            draw ++;

            }else if(player[i]-com[i]<0)
            {
              Com1 ++;

            }else{ 
                Player1++;


            }
        System.out.println("Player"+ Player1+"com"+ Com1 + "Draw"+ draw);
        }

    }else if(input == 2){
        System.out.println("Game Mode : input the way you want , three time");
    }else{
        System.out.println("fatal error , game will be close");
    }
}

我尝试了很多次,但是有输出。

这是一个摇滚,纸张,剪刀游戏。

但没有输出, 只有线程"main" java.util.InputMismatchException

中的异常

1 个答案:

答案 0 :(得分:0)

它在我的最终工作原样。如果您未向其提供预期输入(例如,在期望整数时提供实数),则会发生异常java.util.InputMismatchException。除此之外,它成功执行。

另外,作为建议,您应该考虑改进问题的主题并正确格式化问题。