Java bluej中石头、布、剪刀游戏中的阵列
我的代码有两个问题。我正在编写一个程序,模拟两个用户之间玩的石头、布、剪刀游戏。获胜者由计算机生成的五轮中的最佳一轮决定 三项要求:Java bluej中石头、布、剪刀游戏中的阵列,java,arrays,bluej,Java,Arrays,Bluej,我的代码有两个问题。我正在编写一个程序,模拟两个用户之间玩的石头、布、剪刀游戏。获胜者由计算机生成的五轮中的最佳一轮决定 三项要求: 您必须编写一个循环来运行游戏的五轮,将值分配到两个数组中,不允许平局 你必须写一个循环来确定每个玩家的获胜次数 必须编写循环以打印每个数组中的值(映射到单词) 我需要第一个需求中的“不允许绑定”,以及将单词映射到第三个需求中要打印的数组中的值的帮助 所以,我需要这些阵列: 球员1 1 0 1 2 2 玩家2 2 1 0 1 0 看起来像这样: 玩家1玩家2 剪纸
for(index=0; index<5; index++){
player1[index]=random.nextInt(3);
player2[index]=random.nextInt(3);
for(index=0;indexplayer1[index]=random.nextInt(3);
正在用整数填充数组
if((player1[index]='0')&(player2[index]='2')){
正在比较数组,就好像它们包含字符一样
尝试if((player1[index]==0)和&(player2[index]==2)){
etc(只需删除单引号)。对于第三个问题,使用类似以下字符串的数组:
String [] names = new String[3];
names[0] = "Rock";
names[1] = "Paper";
names[2] = "Scissor";
然后将整数值[0,2]映射到字符串数组,如:
for(index = 0; index < 5; index++){
System.out.println("\t\t" + names[player1[index]] + "\t" + names[player2[index]]);
}
for(索引=0;索引<5;索引++){
System.out.println(“\t\t”+名称[player1[index]]+”\t”+名称[player2[index]]);
}
对于第一个问题,只需删除单引号,您需要将数组中的值与整数类型进行比较,但将整数与字符类型进行比较,您不会得到编译时错误,但逻辑上不正确。For(int j=0;j player2[j]){//检查播放器1是否占上风
for(int j = 0; j <5; j++){
if(player1[j] > player2[j]){// checks to see if player 1 has the upper hand
play1++;// if so bam hers a point for the win
}
else if(player1[j] < player2[j]){ //if player 2 has the upper hand bam gets a point
play2++;
}
if(player1[j] == player2[j]){ // meh if they are tied.. do it again!
while(player1[j] == player2[j]){ // this is where you are having trouble
//use this a psudo code to get an idea of how to steer your problematic thinking
player1[j] = ran.nextInt(3);
player2[j] = ran.nextInt(3);
}
play1++;//如果是这样的话,那么她赢了一分
}
否则如果(玩家1[j]<玩家2[j]){//如果玩家2占上风,bam得一分
play2++;
}
如果(player1[j]==player2[j]){//meh如果他们打成平局..再来一次!
while(player1[j]==player2[j]){//这就是您遇到问题的地方
//使用此psudo代码了解如何引导您的问题思维
player1[j]=ran.nextInt(3);
player2[j]=ran.nextInt(3);
}
如果您不希望两个值在写入当前值的同时保持相同,以获得新的数字,直到它们不相等为止对于第一个问题,只需删除单引号,您需要将数组中的值与整数类型进行比较,但将整数与字符类型进行比较,您不会得到编译时错误,但在逻辑上是不正确的非常感谢你的帮助!关于第一个问题,在我删除单引号后,我如何防止它允许平局?在第一个循环中,当数字被随机分配时,有没有办法防止平局,所以如果玩家1得到0,玩家2必须得到1或2?是的,你可以这样做,为第一个玩家获得随机数。Th当两个数字相等时,为第二个玩家获取随机数。
for(index = 0; index < 5; index++){
System.out.println("\t\t" + names[player1[index]] + "\t" + names[player2[index]]);
}
for(int j = 0; j <5; j++){
if(player1[j] > player2[j]){// checks to see if player 1 has the upper hand
play1++;// if so bam hers a point for the win
}
else if(player1[j] < player2[j]){ //if player 2 has the upper hand bam gets a point
play2++;
}
if(player1[j] == player2[j]){ // meh if they are tied.. do it again!
while(player1[j] == player2[j]){ // this is where you are having trouble
//use this a psudo code to get an idea of how to steer your problematic thinking
player1[j] = ran.nextInt(3);
player2[j] = ran.nextInt(3);
}