我必须制作一个纸质摇滚剪刀程序,让用户输入一个选项,然后根据计算机的选择进行测试。在每场比赛之后,它应该询问球员是否要继续,并且他们应该输入'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);
}
}
}
当我运行它时,循环运行正常,游戏播放,但随后我得到一个“超出索引范围的字符串”错误。知道如何解决这个问题吗?
答案 0 :(得分:2)
您的nextInt()
只是从输入缓冲区中读取数字,并在其中留下新行。因此,当您拨打input.nextLine()
时,您将获得一个空行 - 该号码后面的第一行的其余部分。你应该阅读下一行并确保它不是空的。如果是,请再次阅读。
顺便提一下,你的代码确定谁赢了有点麻烦。如果我是你,我会尽量让它变得更加通用和干净。考虑一个可以处理更复杂游戏的解决方案,例如Rock Paper Scissors Lizard Spock,而不会添加太多代码。
答案 1 :(得分:1)
当您从用户那里得到答案时,您不会阅读下一行,因此扫描仪仍然有一个换行符号。然后,当您阅读nextline
时,您会读到该新行,因此没有charat(0)
。
变化:
cont = input.nextLine().charAt(0);
为:
cont = input.next().charAt(0);
答案 2 :(得分:-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*/
}
}
}