java Java石头剪刀布循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12735915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 10:01:09  来源:igfitidea点击:

Java rock paper scissors loop

java

提问by user1721173

I'm having to make a paper rock scissors program that has the user enter in a choice, then tests it against the computer's choice. After every game, it should ask the player if they want to continue, and they should enter in 'Y' or 'N' to continue or quit. The best I could think was a while loop, and everything works fine except the very last bit.

我必须制作一个纸剪刀程序,让用户输入一个选择,然后根据计算机的选择对其进行测试。每场比赛结束后,它应该询问玩家是否要继续,他们应该输入'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);
        }       
    }
}

When I run it, the loop runs fine, the game is played, but then I get a 'string out of index range' error. Any idea how to resolve this?

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

回答by zmbq

Your nextInt()just reads the number from the input buffer, leaving the new line in it. So when you call input.nextLine()you're getting an empty line - the rest of the first line after the number. You should read the next-line and make sure it's not empty. If it is, just read it again.

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

Incidentally, your code that figures out who won is a bit cumbersome. If I were you, I would try to make it a little more general and clean. Think about a solution that can handle a more complex game, such as Rock Paper Scissors Lizard Spockwithout adding too much code.

顺便说一下,您计算出谁赢了的代码有点麻烦。如果我是你,我会尽量让它更一般和干净。考虑一种可以处理更复杂游戏的解决方案,例如Rock Paper Scissors Lizard Spock,而无需添加太多代码。

回答by NominSim

When you get the answer from the user, you don't read the next line so the scanner still has a new line character. Then when you read the nextlineyou read that new line, and therefore there is no charat(0).

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

Change:

改变:

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

to:

到:

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

回答by Allen

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*/

     }
    }
}