簡體   English   中英

Java Math.random返回0?

[英]Java Math.random returning 0?

我知道這個問題已經回答過了,我看過這里: math.random,僅生成0? 以及其他一些論壇,但到目前為止無法回答我的問題。

我正在嘗試為Rock Paper Scissors游戲產生1到3之間的隨機數(包括1和3)。 不幸的是,我的隨機數的輸出始終為0。

請注意,我不允許使用Random(); 類,並且必須使用Math.random方法來實現。 這是我的隨機數代碼:

  public static int randomComputerChoice(int highVal, int lowVal) {
         do{
             highVal = 4;
             lowVal = 0;
     computerChoice=0;//resets random number
   //generates and returns a random number within user's range 
     computerChoice = (int)((Math.random()* highVal)+1);
     } while((computerChoice>= highVal)||(computerChoice<= lowVal));
     return (computerChoice);
} 

}

我不確定這是否必要,但是我覺得錯誤可能出在我的其余代碼中。

import java.util.Scanner;
import java.io.IOException;

public class RockPaperScissors {

    static int weaponChoice;
    static int computerChoice;
   static int lowVal = 1;
   static int highVal = 3;

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        Scanner userInput = new Scanner(System.in);
        System.out.println(" =========================");
        System.out.println("====ROCK=PAPER=SCISSORS====");
        System.out.println(" =========================");
        int playAgain = 1; //sets a play again function
       do {
            System.out.println("Please select your weapon.");
            System.out.println("1 - Rock");
            System.out.println("2 - Paper");
            System.out.println("3 - Scissors");
            System.out.println("Choose wisely:");
            String choice = userInput.nextLine();
            weaponChoice = Integer.parseInt(choice);
            do {
           if (weaponChoice != 1 && weaponChoice != 2 && weaponChoice != 3) {
                    System.out.println("Please choose again, grasshopper. There are only"
                        + " three choices.");
                System.out.println("1 - Rock");
                System.out.println("2 - Paper");
                System.out.println("3 - Scissors");
                choice = userInput.nextLine();
                weaponChoice = Integer.parseInt(choice);
            }
            } while (weaponChoice != 1 && weaponChoice !=2 && weaponChoice != 3);

            if (weaponChoice == 1) {
                System.out.println("You have selected Rock as your weapon.");
            }
            else if (weaponChoice == 2) {
                System.out.println("You have selected Paper as your weapon.");
            }
            else {
                System.out.println("You have selected Scissors as your weapon.");
            }
            //Computer's Choice
             System.out.println("The computer's choice is "+computerChoice);


        } while (playAgain == 1);
}

   public static int randomComputerChoice(int highVal, int lowVal) {
         do{
             highVal = 4;
             lowVal = 0;
     computerChoice=0;//resets random number
   //generates and returns a random number within user's range 
     computerChoice = (int)((Math.random()* highVal)+1);
     } while((computerChoice>= highVal)||(computerChoice<= lowVal));
     return (computerChoice);
} 


} 

謝謝你的幫助! 非常感謝。

試試: Min + (int)(Math.random() * ((Max - Min) + 1)) 輸出應為1到3之間的數字。最小為1,最大為3。 而已。 該數字范圍應為:[1,3],這意味着包括1和3。

這是另一個建議:

int number = 1 + (int)(Math.random() * ((3 - 1) + 1));
    System.out.println(number);
//alternative way:
// return number;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM