Java 爪哇石头剪刀环
我必须制作一个剪刀布程序,让用户输入一个选项,然后根据计算机的选择进行测试。每场比赛结束后,它应该询问玩家是否想继续,他们应该输入“Y”或“N”继续或退出。我所能想到的最好的方法是一个while循环,除了最后一点之外,一切都很好Java 爪哇石头剪刀环,java,Java,我必须制作一个剪刀布程序,让用户输入一个选项,然后根据计算机的选择进行测试。每场比赛结束后,它应该询问玩家是否想继续,他们应该输入“Y”或“N”继续或退出。我所能想到的最好的方法是一个while循环,除了最后一点之外,一切都很好 import java.util.Scanner; public class rockpaperscissors { public static void main(String[] args) { Scanner input = new S
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);
}
}
}
当我运行它时,循环运行正常,游戏开始了,但是我得到一个“字符串超出索引范围”错误。知道如何解决这个问题吗?您的nextInt()
只需从输入缓冲区读取数字,将新行保留在其中。因此,当您调用input.nextLine()
时,您将得到一个空行,即数字后面第一行的其余部分。你应该读下一行,确保它不是空的。如果是,就再读一遍
顺便说一句,你的代码,找出谁赢了是有点麻烦。如果我是你的话,我会尽量使它更一般和干净一点。考虑一个可以处理更复杂游戏的解决方案,例如不添加太多代码。当您从用户那里得到答案时,您不会读取下一行,因此扫描仪仍然有一个新行字符。然后,当您阅读
下一行时
会阅读该新行,因此没有字符(0)
更改:
cont = input.nextLine().charAt(0);
致:
包装纸;
导入java.util.Scanner;
/**
*
*@作者艾伦E。
*/
公营石板纸{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
int rock=0;
整版纸=1;
int=2;
int user=0;
int计算机=0;
int gamesplayed=0;
扫描仪扫描=新扫描仪(System.in);
同时(游戏显示<3)
{
System.out.println(“石头=0,布=1,剪刀=2”);
字符串userinput=scan.nextLine();
int convertinput=Integer.valueOf(userinput);
int Computerinput=(int)(Math.random()*3);
如果(Computerinput==1&&convertinput==0)
{
System.out.println(“纸张击败岩石”+
“\n计算机赢了”);
gamesplayed++;
计算机++;
}
else if(convertinput==1&&Computerinput==0)
{
System.out.println(“纸张击败岩石”+
“\n您赢了!”);
gamesplayed++;
用户++;
}
如果(Computerinput==0&&convertinput==2)
{
System.out.println(“石头胜过剪刀”+
“\n计算机赢了”);
gamesplayed++;
计算机++;
}
else if(convertinput==0&&Computerinput==2)
{
System.out.println(“石头胜过剪刀”+
“\n您赢了!”);
gamesplayed++;
用户++;
}
如果(Computerinput==2&&convertinput==1)
{
System.out.println(“剪刀胜过纸”+
“\n计算机赢了”);
gamesplayed++;
计算机++;
}
else if(convertinput==2&&Computerinput==1)
{
System.out.println(“剪刀胜过纸”+
“\n您赢了”);
gamesplayed++;
用户++;
}
/*************************************************
* *
* *
*打领带*
* *
* *
*************************************************/
如果(Computerinput==0&&convertinput==0)
{
System.out.println(“岩石系岩石”+
“\nTie”);
}
如果(Computerinput==1&&convertinput==1)
{
System.out.println(“纸系纸”+
“\nTie”);
}
如果(Computerinput==2&&convertinput==2)
{
System.out.println(“剪刀结剪刀”+
“\nTie”);
}/*While循环结束*/
}
}
}
cont = input.next().charAt(0);
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*/
}
}
}