在bluej的岩石,纸,剪刀游戏中的数组

时间:2014-11-12 02:14:28

标签: java arrays bluej

我的代码有两个问题。我正在编写一个程序,模拟两个用户之间玩的Rock,Paper,Scissors游戏。获胜者由五轮计算机生成的最佳轮次决定。

三个要求:

  1. 你必须编写一个循环来运行五轮游戏,将值分配到两个数组中,不允许任何关系。
  2. 你必须写一个循环来确定每个玩家的胜利数量。
  3. 您必须编写一个循环来打印每个数组中的值(映射到单词)。
  4. 我需要帮助"不允许任何关系"在第一个要求中,将单词映射到数组中的值以在第三个要求中打印。

    所以,我需要这些数组:

    球员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; index<5; index++){
            if((player1[index]==0) && (player2[index]==2)){
                player1Win++;
            }else if((player1[index]==1) && (player2[index]==0)){
                player1Win++;
            }else if((player1[index]==2) && (player2[index]==1)){
                player1Win++;
            }else{
                player2Win++;
            }
        }
    

    第三循环:

        for(index=0; index<5; index++){
            System.out.print("\t\t " + player1[index] + "");
            System.out.println("\t  " + player2[index] + "");
        }
    

    谢谢!

3 个答案:

答案 0 :(得分:0)

player1[index]=random.nextInt(3);用数组填充数组。

if((player1[index]=='0') && (player2[index]=='2')){正在比较您的数组,就像它们包含字符一样。

尝试if((player1[index]==0) && (player2[index]==2)){等(只需删除单引号)。

答案 1 :(得分:0)

对于第三个问题,请使用String数组:

String [] names = new String[3];
names[0] = "Rock";
names[1] = "Paper";
names[2] = "Scissor";

然后将整数值[0,2]映射到String数组,如:

for(index = 0; index < 5; index++){
    System.out.println("\t\t" + names[player1[index]] + "\t" + names[player2[index]]);
}

对于第一个问题,只需删除单引号,您需要将数组中的值与整数类型进行比较,但是您要将整数与字符类型进行比较,您不会得到编译时错误,但逻辑上不正确。

答案 2 :(得分:0)

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

如果您不希望两个值相同,只需覆盖当前值以获取新数字,直到它们不相等为止