gpt4 book ai didi

java - bluej 中的石头、剪刀、布游戏中的数组

转载 作者:太空宇宙 更新时间:2023-11-04 14:23:56 25 4
gpt4 key购买 nike

我的代码有两个问题。我正在编写一个程序来模拟两个用户之间玩的石头、剪刀、布游戏。获胜者由计算机生成的五轮中最好的一轮决定。

三个要求:

  1. 您必须编写一个循环来运行游戏的五轮,将值分配到两个数组中,不允许出现平局。
  2. 您必须编写一个循环来确定每个玩家的获胜次数。
  3. 您必须编写一个循环来打印每个数组中的值(映射到单词)。

我需要有关第一个要求中的“不允许任何关系”的帮助,以及将单词映射到数组中的值以在第三个要求中打印的帮助。

所以,我需要这些数组:

玩家 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] + "");
}

谢谢!

最佳答案

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

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

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

关于java - bluej 中的石头、剪刀、布游戏中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26878108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com