【问题标题】:Random number keeps giving the number 1随机数不断给出数字 1
【发布时间】:2015-04-22 13:57:12
【问题描述】:

自从我在我的小石头剪刀布游戏中添加了 while 循环以来,它一直给我数字 1。

package steenpapierschaar1;

import java.util.Scanner;

/**
 *
 * @author T
 */
public class SteenPapierSchaar1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        int rock = 1 ;   // waarde van Rock
        int paper = 2;  // waarde van paper
        int scissor = 3;// waarde van scissor
        int AI = 1 + (int) (Math.random() *3);// maakt int AI een random getal nog niet af moet een range in komen
        Scanner KeyBoard = new Scanner (System.in);// naam scanner input
        boolean playing = true;
        String answer, answer2 = null;


        while(playing = true){
            System.out.println("choose 1 for rock, 2 for paper and 3 for scissor"); // string met keuze voor User
            int UserChoice = KeyBoard.nextInt();// waarde van UserChoice

            if (AI == 1 && UserChoice == 2 || AI == 2 && UserChoice == 3 || AI == 3 && UserChoice == 1) { // als de speler elke keer 1x hoger heeft wint hij de ronde
                System.out.println("You WIN");// outprint met dat je gewonnen hebt en wat de computer doet
                if (AI == 1)
                    System.out.println("The Computer did rock");
                if (AI == 2)
                    System.out.println("The Computer did paper");
                if (AI == 3)
                    System.out.println("The Computer did scissors");


            }

            else if (AI == 1 && UserChoice == 1 || AI == 2 && UserChoice == 2 || AI == 3 && UserChoice == 3) {
                System.out.println("Draw");
                if (AI == 1)
                    System.out.println("The Computer did rock");
                if (AI == 2)
                    System.out.println("The Computer did paper");
                if (AI == 3)
                    System.out.println("The Computer did scissors");
            }

            else if (AI == 1 && UserChoice == 3 || AI == 2 && UserChoice == 1 || AI == 3 && UserChoice == 2){
                System.out.println("You Lose");
                if (AI == 1)
                    System.out.println("The Computer did rock");
                if (AI == 2)
                    System.out.println("The Computer did paper");
                if (AI == 3)
                    System.out.println("The Computer did scissors");
            }              

        }        
    }
}

【问题讨论】:

  • 好吧,我先使用java.util.Random 而不是Math.random()。然后我会修复所有变量等以遵循 Java 命名约定,并整理缩进...
  • 看看你在哪里给AI赋值——你在循环之前这样做。 (顺便说一下,当您有很多与&&|| 相关的条件时,我强烈建议您添加括号,以使分组非常清晰。您还应该尝试重构代码......相同的代码行显示计算机的移动三遍...)
  • 我建议使用 enum 代替 int 和 switch 代替 ifs,check
  • 另外,你的while循环:while(playing = true),你基本上是赋值play而不是比较,用==比较boolean,不过这种情况下while(playing)就足够了。

标签: java random int


【解决方案1】:

您的代码几乎没有问题。您几乎没有未使用的变量,例如

int rock = 1 ;  // waarde van Rock
int paper = 2;  // waarde van paper
int scissor = 3;// waarde van scissor

应该是

final int ROCK = 1; // waarde van Rock
final int PAPER = 2; // waarde van paper
final int SCISSORS = 3;// waarde van scissor

和使用一样

if (AI == ROCK) ...

其他问题是while (playing == true) 注意= 是赋值运算符== 是比较运算符。所以在你的循环中,你实际上将true 分配给playing,然后它总是被评估为true

为了避免错误写=而不是==的问题,只需使用

while (playing){

}

顺便说一句,您可能应该在每次玩游戏后询问用户是否要继续/退出游戏。您也可以通过检查像 -1 这样的值来表示“退出”。


另一个问题是你永远不会在循环内重新分配AI 的值,因此它的值不能改变并且与程序开始时相同。所以也许移动你的

int AI = 1 + (int) (Math.random() * 3);

在您的while 循环开始时让AI 获得新值。

BTW2 而不是 Math.random()*3 这有点神秘,您可以使用 Random 类及其 nextInt(n) 方法,它将随机范围内的任何整数 [0; n)

  • 0(含)
  • n(不包括)。

也可以解释为[0; n-1]。所以你的代码可能看起来像

Random r = new Random();
while(..){
    int AI = r.nextInt(3) + 1; // +1 because we want to move range [0; 2] to [1; 3]
    ...
}

看起来你正在调用

if (AI == 1)
    System.out.println("The Computer did rock");
if (AI == 2)
    System.out.println("The Computer did paper");
if (AI == 3)
    System.out.println("The Computer did scissors");

在您的每个赢/平/输条件中。在这种情况下,您可以简单地将其移出每个块,例如

if (win conditions){
    print win
}else if (draw condition){
    print draw
}else if (lose condition){
    print lose
}
<move code drawing computer hand here>

绘制条件看起来过于复杂。看起来当AIUserChoice 相等时就实现了draw,那么简单

if (AI == UserChoice)

可以替换

if (AI == 1 && UserChoice == 1 || AI == 2 && UserChoice == 2 || AI == 3 && UserChoice == 3)

您还可以简化您的其他条件使用模% 运算符。

【讨论】:

    【解决方案2】:

    包括java.util.Random 并做这两件事。

    使用这个功能:

    public static int randAI() {
        Random rand = new Random();
    
        int randomNum = rand.nextInt((3 - 1) + 1) + 1;
    
     return randomNum;
    }
    

    并将其添加到您的循环中:

    while(playing = true){
     AI = randAI();
     //REST of your code
    

    这样你的人工智能将在每次迭代中随机化。

    【讨论】:

      【解决方案3】:

      因为Math.random() 将给出介于0.0 to 1.0 之间的值。

      在大多数情况下是这样

       int AI = 1 + (int) (Math.random() *3);
      

      对于您的子指令(int) (Math.random() *3),在大多数情况下,值将是0,因此AI 将是1

      javaDoc 中提到的

      公共静态双随机()
      返回一个带正号的双精度值,大于或等于 0.0 且小于 1.0。返回值是伪随机选择的,从该范围(近似)均匀分布。

      你可以使用java.util.Random代替这个

      【讨论】:

        【解决方案4】:

        您永远不会为 AI 设置新值。

        因此,根据您在程序启动期间将其设置为的值,始终会重复使用此值。

        还要确保初始化随机数生成器

        Random rand = new Random();
        

        然后在你的循环中,你可以调用

        int randomNumber = rand.nextInt((max - min) + 1) + min;
        

        最小值和最大值设置为您的范围。

        有关详细说明,请参阅 How do I generate random integers within a specific range in Java?

        【讨论】:

          【解决方案5】:

          为了回答您的问题,AI 不会在 while 循环中更新。只需将 AI 变量初始化移动到 while 循环中。

          int AI = 0;
          while(playing = true){
                  AI = 1 + (int) (Math.random() *3);
                  System.out.println("choose 1 for rock, 2 for paper and 3 for scissor");
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-07-25
            相关资源
            最近更新 更多