第二个方法调用将不会执行(java)

时间:2013-10-08 21:50:01

标签: java methods dice

我正在尝试制作一个简单的骰子程序,用户输入他们想要掷骰子的边数和骰子数量,并为每个骰子输出一个掷骰子。我已经包含了一个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;
}

}

3 个答案:

答案 0 :(得分:0)

问题在于

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

在打印卷筒的同时,将d增加到,例如4。当此方法结束时,您的while循环开始播放,您输入"y"。再次调用该方法时,d的值为4,为什么t的值为4d == tfor。您需要重置它或使用正确的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]);

    }
}

漂亮的方式是

x

现在你摆脱了cstatic t个变量。您还可以通过一些简单的重构来摆脱sides


顺便说一下,您没有使用{{1}}。

答案 1 :(得分: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++;
    }
}

cd没有理由应该是静态类变量。相反,您应该在方法调用中声明它们,就像使用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++;
    }
}

答案 2 :(得分:0)

你在循环

的情况下尝试这种逻辑
 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()的逻辑:你需要重置那里的值