-1
這是我的代碼: -如何修復java.util.InputMismatchException
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 2或3嗎?如果你給一個字符輸入,這個異常可能發生! –