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

当我运行它时,循环运行良好,游戏已开始,但随后出现“字符串超出索引范围”错误。知道如何解决这个问题吗?

4

3 回答 3

2

nextInt()只需从输入缓冲区中读取数字,并将新行留在其中。因此,当您打电话时,input.nextLine()您会得到一个空行 - 号码后第一行的其余部分。您应该阅读下一行并确保它不为空。如果是,请再读一遍。

顺便说一句,您计算谁获胜的代码有点麻烦。如果我是你,我会尽量让它更一般和更干净。考虑一个可以处理更复杂游戏的解决方案,例如石头剪刀布蜥蜴Spock,而无需添加太多代码。

于 2012-10-04T21:08:48.870 回答
1

当您从用户那里得到答案时,您不会阅读下一行,因此扫描仪仍然有一个换行符。然后,当您阅读时,nextline您会阅读该新行,因此没有charat(0).

改变:

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

至:

cont = input.next().charAt(0);
于 2012-10-04T21:10:34.207 回答
-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*/

     }
    }
}
于 2013-03-16T17:19:48.270 回答