0

我正在尝试制作一个简单的骰子程序,该程序接受用户输入他们想要滚动的边数和骰子数量,并为每个骰子输出一个滚动。我已经包含了一个 while 循环,它允许用户以相同的面数重新滚动相同数量的骰子,而无需重新输入他们之前输入的信息。我遇到的问题是,当我在“while”循环的“if”语句中调用“q”方法时,骰子不会重新滚动。建议?:

    import java.util.Random;
    import java.util.Scanner;

    public class Choice_Dice { 
static int t = 0, sides, c=0, d =0;
public static void main(String [] Mack){
    Scanner scan = new Scanner(System.in);
    String y ="y";
    System.out.println("You may roll a dice with any number of sides");
    System.out.println("Enter the number of sides you would like to the dice to have: ");
    sides = scan.nextInt();
    System.out.println("Enter the number of dice you want to roll: ");
    t = scan.nextInt();
    q();
    while(y.equals("y"))
    {
        System.out.println("Would you like to roll again(y or n): ");
        y = scan.next();
        if(y.equals("y")){
            q();
        }
        else
            System.out.println("Thanks");
    }
}
public static void q()
{
    int[] x = new int[t];
    c = 0;
    while(c != t)
    {
        x[c] = roll(sides);
        c++;
    }
    while(d != t)
    {
        System.out.println("You rolled " + x[d]);
        d++;
    }
}
public static int roll(int s)
{
    Random generator = new Random();
    int dice = 0;
    dice = generator.nextInt(4) + 1;
    return dice;
}

}

4

3 回答 3

0

问题在于

while(d != t)
{
    System.out.println("You rolled " + x[d]);
    d++;
}

d在打印卷的同时,您一直递增到 4。当这个方法结束时,你的while循环开始发挥作用,你输入"y". 再次调用该方法时,d有 的值4,为什么t也有4这样的值d == t。您需要重置它或使用适当的for循环进行迭代。

丑陋的方法是

public static void q()
{
    int[] x = new int[t];
    c = 0;
    while(c != t)
    {
        x[c] = roll(sides);
        c++;
    }
    while(d != t)
    {
        System.out.println("You rolled " + x[d]);
        d++;
    }
    d = 0;
}

漂亮的方法是

public static void q()
{
    int[] x = new int[t];

    for (int i = 0; i < x.length ; i++) {
        x[i] = roll(sides);
        System.out.println("You rolled " + x[i]);

    }
}

所以现在你摆脱了xc static变量。t你也可以通过一些简单的重构来摆脱。


顺便说一句,你没有使用sides

于 2013-10-08T21:58:24.337 回答
0

你试试这个逻辑的情况下while循环

 while(true)
{
    System.out.println("You may roll a dice with any number of sides");
    System.out.println("Enter the number of sides you would like to the dice to have: ");
    sides = scan.nextInt();
    System.out.println("Enter the number of dice you want to roll: ");
    t = scan.nextInt();
    q();

    System.out.println("Would you like to roll again(y or n): ");
    y = scan.next();
    if(y.equals("n")){

        System.out.println("Thanks");
        break;
    }


}

还要检查方法 q() 的逻辑:您需要在那里重置值

于 2013-10-08T22:09:45.233 回答
0

这取决于您对d. 您记得重置 的值c,但没有d

public static void q()
{
    // Init variables
    int[] x = new int[t];
    c = 0;
    d = 0;

    while(c != t)
    {
        x[c] = roll(sides);
        c++;
    }
    while(d != t)
    {
        System.out.println("You rolled " + x[d]);
        d++;
    }
}

没有理由c并且d应该是静态类变量。相反,你应该在你的方法调用中声明它们,就像你做的那样x

public static void q()
{
    // Init variables
    int[] x = new int[t];
    int c = 0;
    int d = 0;

    while(c != t)
    {
        x[c] = roll(sides);
        c++;
    }
    while(d != t)
    {
        System.out.println("You rolled " + x[d]);
        d++;
    }
}
于 2013-10-08T22:00:19.067 回答