使用课程将骰子掷骰随机化

时间:2018-11-11 06:14:56

标签: java

我正在编写一个骰子游戏程序,该程序为用户和计算机创建一个骰子,然后询问用户名,骰子的边数以及用户希望在计算机上玩多少次迭代。然后在每次掷骰之后检查程序,看看哪个掷骰更高,在迭代结束时,它会写回每个用户和计算机的总赢额以及获胜者是谁。我遇到的问题是,我需要创建一个roll()类来随机将骰子设置为用户滚动和计算机滚动的值,并且出现此错误...

Exception in thread "main" java.lang.IllegalArgumentException: bound must be positive
at java.util.Random.nextInt(Random.java:388)
at classwork6_3.Die.roll(Die.java:52)
at classwork6_3.ClassWork6_3.main(ClassWork6_3.java:29)

这是我的main()课...

package classwork6_3;
import java.util.*;

public class ClassWork6_3 {

public static void main(String[] args) {

    Scanner s = new Scanner(System.in);
    Random r = new Random();

    System.out.print("Enter user's name: ");
    String name = s.nextLine();
    int value = 0;
    int value2 = 0;
    System.out.print("How many sides does the die have?: ");
    int sides = s.nextInt();
    s.nextLine();
    System.out.print("How many times would you like to roll?: ");
    int numRoll = s.nextInt();
    int counter = 0;
    int counter2 = 0;

    Die user = new Die(name, sides, value);
    Die comp = new Die(value);

    for(int i = 1; i<= numRoll; i++){

        value = r.nextInt(user.roll(sides)) + 1;
        value2 = r.nextInt(comp.roll(sides)) + 1;

        System.out.printf("%s rolled: %d\n", user.getOwner(), user.getValue());
        System.out.print("Computer rolled: " + comp.getValue() + "\n\n");

            if(value > value2){
                counter++;
            } else if(value2 > value){
                counter2++;
            }

   }
            System.out.printf("%s TOTAL WINS: %d\n", user.getOwner(), counter);
            System.out.print("Computer TOTAL WINS: " + counter2 + "\n\n");
                if(counter > counter2){

                    System.out.printf("%s wins!!\n", user.getOwner());
                }else if(counter2 > counter){

                    System.out.print("Computer wins\n");
                }else{

                    System.out.print("It's a tie!\n");
                }

}

}

这是我的Die()课...

package classwork6_3;
import java.util.*;


public class Die {

Random r = new Random();

private int sides;
private int value;
private String owner;


public Die(String owner, int sides, int value){

    this.sides = sides;
    this.value = value;
    this.owner = owner;
}
public Die (int value){

    this.value = value;
}

public int getSides(){

    return sides;
}
public int getValue(){

    return value;
}
public String getOwner(){

    return owner;
}
public void setSides(int sides){

    this.sides = sides;
}
public void setValue(int value){

    this.value = value;
}
public void setOwner(String owner){

    this.owner = owner;
}
public int roll(int rand){

    rand = r.nextInt(value);

    return value;
}


}

2 个答案:

答案 0 :(得分:1)

这是您的代码存在的问题:

  1. 您永远不会返回roll(...)方法

    生成的随机值
    public int roll(int rand){
        rand = r.nextInt(value);    
        return value;
    }
    

    将其更改为

    public int roll(int rand) {         
        return r.nextInt(rand);     
    }
    
  2. 在for循环中,您只需要调用roll() 方法。由于它已经计算了模具的随机值,因此您无需再次调用r.nextInt()

    value = r.nextInt(user.roll(sides)) + 1;
    value2 = r.nextInt(comp.roll(sides)) + 1;
    

    将其更改为

    value = user.roll(sides) + 1;
    value2 = comp.roll(sides) + 1;
    
  3. 现在使用以下命令打印出值:

    System.out.printf("%s rolled: %d\n", user.getOwner(), value);
    System.out.print("Computer rolled: " + value2 + "\n\n");
    

执行上述步骤后,您的代码将按预期工作。另外请注意,请勿使用含义不明确的变量名称,例如 value value2 等。

答案 1 :(得分:0)

每当您制作一个新的骰子时,value都不会从零更改,导致弹出该错误,因为根据您的Die类,每次调用roll()时它将遍历的范围,是0到0。将value更改为用户输入的值并制作新骰子。