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