我怎样才能让代码重试?
How can I make the code to try again?
这是我的代码..我认为我没有正确使用 do/while 循环...
如何正确使用 do/while?
我应该使用不同的方法吗?
我想知道怎么做
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
System.out.print("enter minimum of Yahtzee dice 2-9:");
int min = input.nextInt();
System.out.print("enter maximum of Yahtzee dice 3-10:");
int max = input.nextInt();
String tryAgain = "y";
do {
System.out.println("Dice Rolls\tYahtzee's Percentage Odds\t");
for (int i = min; i <= max; i++) {
int count = 0;
boolean success = true;
for (int j = 0; j <= 5000000; j++) {
Random generator = new Random();
YahtzeeDice d = new YahtzeeDice(generator, i);
if (d.IsAYahtzee()) {
count++;
}
}
System.out.printf("%d ", i);
System.out.printf("%d\t", 5000000);
System.out.printf("%d\t ", count);
System.out.printf("%f ", (double) count / 5000000);
double per = count / (double) 5000000;
int odds = (int) (1 / per);
System.out.printf("1 in %d\n", odds);
}
System.out.print("Run another simulation [y/n]? ");
tryAgain = input.nextLine();
} while(!tryAgain.equals("n"));
immibis 在第一条评论中就把它钉牢了,我想。你非常接近正确。试试这个:
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
Random generator = new Random();
String tryAgain = "y";
do {
System.out.print("enter minimum of Yahtzee dice 2-9:");
int min = input.nextInt();
input.nextLine();
System.out.print("enter maximum of Yahtzee dice 3-10:");
int max = input.nextInt();
input.nextLine();
System.out.println("Dice Rolls\tYahtzee's Percentage Odds\t");
for (int i = min; i <= max; i++) {
int count = 0;
boolean success = true;
for (int j = 0; j <= 5000; j++) {
YahtzeeDice d = new YahtzeeDice(generator, i);
if (d.IsAYahtzee()) {
count++;
}
}
System.out.printf("%d ", i);
System.out.printf("%d\t", 5000000);
System.out.printf("%d\t ", count);
System.out.printf("%f ", (double) count / 5000000);
double per = count / (double) 5000000;
int odds = (int) (1 / per);
System.out.printf("1 in %d\n", odds);
}
System.out.print("Run another simulation [y/n]? ");
tryAgain = input.nextLine();
} while (!tryAgain.equals("n"));
}
ps。在玩的时候,我移动了你的输入,却不知道你想做什么。你应该将它们移出循环,如果这是你想要的。
这是我的代码..我认为我没有正确使用 do/while 循环...
如何正确使用 do/while?
我应该使用不同的方法吗?
我想知道怎么做
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
System.out.print("enter minimum of Yahtzee dice 2-9:");
int min = input.nextInt();
System.out.print("enter maximum of Yahtzee dice 3-10:");
int max = input.nextInt();
String tryAgain = "y";
do {
System.out.println("Dice Rolls\tYahtzee's Percentage Odds\t");
for (int i = min; i <= max; i++) {
int count = 0;
boolean success = true;
for (int j = 0; j <= 5000000; j++) {
Random generator = new Random();
YahtzeeDice d = new YahtzeeDice(generator, i);
if (d.IsAYahtzee()) {
count++;
}
}
System.out.printf("%d ", i);
System.out.printf("%d\t", 5000000);
System.out.printf("%d\t ", count);
System.out.printf("%f ", (double) count / 5000000);
double per = count / (double) 5000000;
int odds = (int) (1 / per);
System.out.printf("1 in %d\n", odds);
}
System.out.print("Run another simulation [y/n]? ");
tryAgain = input.nextLine();
} while(!tryAgain.equals("n"));
immibis 在第一条评论中就把它钉牢了,我想。你非常接近正确。试试这个:
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
Random generator = new Random();
String tryAgain = "y";
do {
System.out.print("enter minimum of Yahtzee dice 2-9:");
int min = input.nextInt();
input.nextLine();
System.out.print("enter maximum of Yahtzee dice 3-10:");
int max = input.nextInt();
input.nextLine();
System.out.println("Dice Rolls\tYahtzee's Percentage Odds\t");
for (int i = min; i <= max; i++) {
int count = 0;
boolean success = true;
for (int j = 0; j <= 5000; j++) {
YahtzeeDice d = new YahtzeeDice(generator, i);
if (d.IsAYahtzee()) {
count++;
}
}
System.out.printf("%d ", i);
System.out.printf("%d\t", 5000000);
System.out.printf("%d\t ", count);
System.out.printf("%f ", (double) count / 5000000);
double per = count / (double) 5000000;
int odds = (int) (1 / per);
System.out.printf("1 in %d\n", odds);
}
System.out.print("Run another simulation [y/n]? ");
tryAgain = input.nextLine();
} while (!tryAgain.equals("n"));
}
ps。在玩的时候,我移动了你的输入,却不知道你想做什么。你应该将它们移出循环,如果这是你想要的。