我不得不做一個紙岩石剪刀程序,用戶在選擇中輸入,然後根據計算機的選擇進行測試。每場比賽之後,它應該詢問球員是否想繼續,他們應該輸入'Y'或'N'來繼續或退出。我能想到的最好的是一個while循環,除了最後一點以外,一切都很好。爪哇搖滾紙剪刀環
import java.util.Scanner;
public class rockpaperscissors {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
char cont = 'y';
while (cont == 'y'){
int com = (int)(Math.random() * 3);
System.out.println("Paper (0), Rock (1), or Scizzors (2)?");
int hum = input.nextInt();
if (com==(hum))
System.out.println("It's a tie!");
else if (hum == 0)
{
if (com == 1)
System.out.println ("You chose paper, computer chose rock You Win!");
else if (com == 2)
System.out.println ("You chose paper, Computer chose scissors You Lose!");
}
else if (hum == 1)
{
if (com == 2)
System.out.println ("You chose Rock, computer chose scissors You Win!");
else if (com == 0)
System.out.println ("You chose Rock, Computer chose paper You Lose!");
}
else if (hum == 2)
{
if (com == 0)
System.out.println ("You chose Scissors, computer chose paper You Win!");
else if (com == 1)
System.out.println ("You chose Scissors, Computer chose rock You Lose!");
}
System.out.println("Would you like to continue? (Y/N)");
cont = input.nextLine().charAt(0);
}
}
}
當我運行它,循環運行良好,玩遊戲時,但後來我得到一個「串出指數範圍」的錯誤。任何想法如何解決這個問題?