簡體   English   中英

我如何使用math.random但只能用於某些值?

[英]How can I use math.random but only for certain values?

這就是我所擁有的(這只是我整個代碼的摘錄):

int num = (int) Math.random()*100;
    switch(num)
    {
    case 0 : compChoice = "R";break;
    case 1 : compChoice = "P";break;
    case 2 : compChoice = "S";break;
    }

我如何才能只得出隨機數0、1或2?

在后面的返回語句中,它表示在此獲取的字母為“空”

如果有幫助,請參見以下完整代碼:

import java.util.Scanner;
import static java.lang.System.*;

public class RockPaperScissors
{
private String playChoice;
private String compChoice;

public RockPaperScissors()
{



}

public RockPaperScissors(String player)
{
    playChoice = player;
}

public void setPlayers(String player)
{
    playChoice = player;
    int num = (int) Math.random()*100 %3;
    switch(num)
    {
    case 0 : compChoice = "R";break;
    case 1 : compChoice = "P";break;
    case 2 : compChoice = "S";break;
    }
    out.print(compChoice);
}

public String determineWinner()
{
    String winner="";

    if(playChoice == "R")
    {
        switch(compChoice)
        {
        case "R" : winner = "!Draw Game!";break;
        case "P" : winner = "!Computer wins <<Paper Covers Rock>>!";break;
        case "S" : winner = "!Player wins <<Rock Breaks Scissors>>!";break;
        }
    }
    else if(playChoice == "P")
    {
        switch(compChoice)
        {
        case "R" : winner = "!Player wins <<Paper Covers Rock>>!";break;
        case "P" : winner = "!Draw Game!";break;
        case "S" : winner = "!Computer wins <<Scissors Cuts Paper>>!";break;
        }
    }
    else if(playChoice == "S")
    {
        switch(compChoice)
        {
        case "R" : winner = "!Computer wins <<Rock Breaks Scissors>>!";break;
        case "P" : winner = "!Player wins <<Scissors Cuts Paper>>!";break;
        case "S" : winner = "!Draw Game!";break;
        }
    }
    return winner;
}

public String toString()
{
    String output="";

    output = "player had " + playChoice + "\n computer had " + compChoice + "\n " + determineWinner();

    return output;
}
}

這是我的跑步者類,因為有人指出我在任何地方都不調用任何方法:

import java.util.Scanner;
import static java.lang.System.*;

public class Lab10d
{
public static void main(String args[])
{
    Scanner keyboard = new Scanner(System.in);
    char response;

    //add in a do while loop after you get the basics up and running

        String player = "";

        out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");

        //read in the player value
        player = keyboard.next();

        RockPaperScissors game = new RockPaperScissors(player);
        game.determineWinner();
        out.println(game);
            while(response == response)
    {
        out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
        player = keyboard.next();
        game.setPlayers(player);
        game.determineWinner();
        out.println(game + "\n");
        out.println("would you like to play again? (y/n):: ");
        response = (char) keyboard.next();




    }

}
}

使用Modulo運算符

int num = (int) Math.random()*100 % 3;

Math.random()返回介於0和1之間的偽隨機值

乘以100使其成為介於0到100之間的數字(帶有十進制值)。

強制轉換為(int)會刪除十進制值-如果您使用的是語言,則與Math.floor()相同。

模數3是潛水的余數:3,例如55/3 = 18余數1(換句話說,(18 * 3)+1 = 55)。

Math.random()返回0到1之間的浮點值。

如果將其乘以3,則會得到0到3之間的浮點值。

如果將浮點值轉換為整數,則將得到0、1或2。

int v = (int) (Math.random() * 3);

如果簡單乘法使您感到害怕,則可以使用java.util.Random類。 你可以做:

/** Re-usable source of random numbers */
private Random rand = new Random();

/** Choose a value of 0, 1, or 2 */
public int myChoice() {
    int v = rand.nextInt(3);
    return v;
}

使用隨機實例可使您插入更高級的隨機數生成器,如果您打算進行隨機實驗,則這是必需的。

根據不同的意見,我想嘗試另一種方法。 我只是將您的代碼復制到我的IDE中,然后運行它。

public class NewMain {
    public static void main(String[] args) {
        int num;
        String compChoice = "";
        for (int ii = 0; ii < 10; ii++) {
            num = (int) (Math.random() * 100) % 3;

            switch (num) {
                case 0:
                    compChoice = "R";
                    break;
                case 1:
                    compChoice = "P";
                    break;
                case 2:
                    compChoice = "S";
                    break;
            }

            System.out.println(num + " " + compChoice);
        }
    }
}

Gregmac的方法還可以,只是缺少一組()。 如果您的代碼實際上是您在上面提供的代碼,並且compChoice始終為null,則存在嚴重錯誤。

我認為這樣做的一個好方法就是將數字乘以四舍五入。 我無法按照你們中的一些人的回答來運作,但是如何呢?:

var a=Math.random()*3;
var num=Math.round(a);
switch(num)
{
case 0 : compChoice = "R";break;
case 1 : compChoice = "P";break;
case 2 : compChoice = "S";break;
}

暫無
暫無

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

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