随机数一直给数字1

Random number keeps giving the number 1

自从我将 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 并做这两件事。

使用这个函数:

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

这样你的 ai 将在每次迭代时随机。

因为 Math.random() 将给出介于 0.0 to 1.0.

之间的值

所以在大多数情况下

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

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

如 javaDoc 中所述

public static double random()
Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.

您可以使用 java.util.Random

而不是这个

您从未为 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?

您的代码中几乎没有问题。你有一些未使用的变量,比如

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){

}

顺便说一句,如果用户想要 continue/quit 游戏,你可能应该在每次玩游戏后询问。您也可以通过检查像 -1 这样的值可以表示 "quit".


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

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

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

BTW2 而不是 Math.random()*3 ,你可以使用 Random class 及其 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");

在您的每个 win/draw/lose 条件中。在这种情况下,您可以简单地将它移出这些块中的每一个,例如

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

绘制条件看起来过于复杂。当 AIUserChoice 相等时,看起来平局就完成了,如此简单

if (AI == UserChoice)

可以取代

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

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

为了回答您的问题,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");