我如何使用math.random但仅限于某些值?

时间:2012-11-07 23:27:18

标签: java random switch-statement

这就是我所拥有的(它只是我整个代码的摘录):

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();




    }

}
}

4 个答案:

答案 0 :(得分:2)

使用Modulo operator

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

Math.random()返回0到1之间的pseudo-random value

乘以100使其成为0到100之间的数字(带小数值)。

转换为(int)会丢弃小数值 - 与Math.floor()相同,如果您的语言是这样的话。

模3是潜水的指挥官3:例如,55/3 = 18余数1(换句话说,(18 * 3)+ 1 = 55)。

答案 1 :(得分:2)

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;
}

使用Random实例允许您插入更高级的随机数生成器,如果您打算进行随机实验,这将是必要的。

答案 2 :(得分:0)

基于不同的评论,我想尝试另一种方法。我只是将你的代码复制到我的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,则会出现严重错误。

答案 3 :(得分:0)

我认为这样做的一个好方法就是在乘以它之后对数字进行舍入。我不能按照你们中的一些人的方式来解决这个问题但是这个怎么样?:

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;
}