0

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

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

4 回答 4

2

使用模运算符

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

于 2012-11-07T23:30:37.067 回答
2

Math.random()返回一个介于 0 和 1 之间的浮点值。

如果将其乘以 3,则会得到一个介于 0 和 3 之间的浮点值。

如果将浮点值转换为整数,则会得到 0、1 或 2。

IEint 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 实例允许您插入更高级的随机数生成器,如果您打算进行随机实验,这将是必要的。

于 2012-11-08T00:11:09.450 回答
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 始终为空,则存在严重错误。

于 2012-11-07T23:52:18.807 回答
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;
}
于 2013-04-17T08:37:55.617 回答