2012-10-04 63 views
2

我不得不做一個紙岩石剪刀程序,用戶在選擇中輸入,然後根據計算機的選擇進行測試。每場比賽之後,它應該詢問球員是否想繼續,他們應該輸入'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); 
     }  
    } 
} 

當我運行它,循環運行良好,玩遊戲時,但後來我得到一個「串出指數範圍」的錯誤。任何想法如何解決這個問題?

回答

2

您的nextInt()只是從輸入緩衝區中讀取數字,並在其中留下新行。所以當你打電話給input.nextLine()時,你會看到一個空行 - 數字後面第一行的其餘部分。你應該閱讀下一行,並確保它不是空的。如果是,請重新閱讀。

順便說一句,你的代碼,誰知道誰贏了有點麻煩。如果我是你,我會盡量讓它更加一般和乾淨。想想一個可以處理更復雜遊戲的解決方案,例如Rock Paper Scissors Lizard Spock,但不添加太多的代碼。

1

當您從用戶那裏得到答案時,您不會閱讀下一行,因此掃描儀仍然具有新的行字符。然後當你閱讀nextline時,你閱讀了新的一行,因此沒有charat(0)

變化:

cont = input.nextLine().charAt(0); 

到:

cont = input.next().charAt(0); 
-1
package rockpaper; 


import java.util.Scanner; 

/** 
* 
* @author Allen E. 
*/ 

public class RockPaper { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     int rock = 0; 
     int paper = 1; 
     int Scissors = 2; 

     int user = 0; 
     int computer = 0; 
     int gamesplayed = 0; 

     Scanner scan = new Scanner(System.in); 

     while (gamesplayed < 3) 
       { 


     System.out.println("Rock = 0 , Paper = 1, Scissors = 2"); 
     String userinput = scan.nextLine(); 

     int convertinput = Integer.valueOf(userinput); 
     int Computerinput = (int)(Math.random()*3); 

     if (Computerinput == 1 && convertinput == 0) 
     { 
      System.out.println("Paper beats Rock " + 
        "\nThe computer won"); 
      gamesplayed++; 
      computer++; 
     } 
     else if (convertinput == 1 && Computerinput == 0) 
     { 
      System.out.println("Paper beats Rock " + 
        "\nYou Win!"); 
      gamesplayed++; 
      user++; 
     } 
    if (Computerinput == 0 && convertinput == 2) 
    { 
     System.out.println("Rock beats Scissors " + 
       "\nThe computer won"); 
     gamesplayed++; 
     computer++; 
    } 
    else if (convertinput == 0 && Computerinput == 2) 
    { 
     System.out.println("Rock beats Scissors " + 
       "\nYou Win!"); 
     gamesplayed++; 
     user++; 
    } 

    if (Computerinput == 2 && convertinput == 1) 
    { 
     System.out.println("Scissors beats Paper " + 
       "\nThe computer won"); 
     gamesplayed++; 
     computer++; 
    } 
    else if (convertinput == 2 && Computerinput == 1) 
    { 
     System.out.println("Scissors beats Paper " + 
       "\nYou Win"); 
     gamesplayed++; 
     user++; 
    } 

    /************************************************* 
     *            * 
     *            * 
     *     Handling a tie    * 
     *            * 
     *            * 
     *************************************************/ 

    if (Computerinput == 0 && convertinput == 0) 
    { 
     System.out.println("Rock ties Rock " + 
       "\nTie"); 

    } 
    if (Computerinput == 1 && convertinput == 1) 
    { 
     System.out.println("Paper ties Paper " + 
       "\nTie"); 

    } 
    if (Computerinput == 2 && convertinput == 2) 
    { 
     System.out.println("Scissors ties Scissors " + 
       "\nTie"); 

    }/*End of While Loop*/ 

    } 
    } 
}