gpt4 book ai didi

java - 结束 while 循环的问题

转载 作者:行者123 更新时间:2023-12-01 11:52:10 29 4
gpt4 key购买 nike

我试图让程序在总数达到 10 时结束,但由于某种原因,我的 while 循环在达到 10 时继续计数。一旦回答了 10 个问题,我就有 int 百分比来查找百分比。

import java.util.*;

CAI 类 { 私有(private)静态扫描仪输入;

public static void main(String[] arguments) {
menu();// calls menu method
compute();// calls compute method
}

public static void menu() {// method that displays menu
System.out.println(" CAI MENU ");
System.out.println("\n)");
System.out
.println("\n1) DIFFICULTY 1\n2) DIFFICULTY 2\n3) DIFFICULTY 3\n4) DIFFICULTY 4");

}

public static int[] Blop() {
Random rand = new Random();
int arr[] = new int[8];
arr[0] = rand.nextInt(9);
arr[1] = rand.nextInt(9);
arr[2] = rand.nextInt(99);
arr[3] = rand.nextInt(99);
arr[4] = rand.nextInt(999);
arr[5] = rand.nextInt(999);
arr[6] = rand.nextInt(9999);
arr[7] = rand.nextInt(9999);
return arr;
}

public static void compute() {
int difficulty;
input = new Scanner(System.in);
System.out.println("Enter an option: ");
difficulty = input.nextInt();
int total = 0;
int percent = 0;
while (total <= 10) {
if (difficulty == 1) {
int num[] = new int[2];
int ans;
String choice;
do {
num = Blop();
do {
System.out.print("How much is " + num[0] + " times "
+ num[1] + " ? :");
total++;
System.out.print(total);
ans = input.nextInt();
String Correct;
String Wrong;
String[] correct = { "Very good! ", "Excellent! ",
"Nice work! ", "Keep up the good work! " };
String[] wrong = { "No. Please try again. ",
"Wrong. Try once more. ", "Don’t give up! ",
"No. Keep trying " };
Random rand = new Random();
Correct = correct[rand.nextInt(correct.length)];
Wrong = wrong[rand.nextInt(wrong.length)];
if (ans == (num[0] * num[1])) {
System.out.print(Correct);
percent++;
} else {
System.out.print(Wrong);
}

} while (ans != (num[0] * num[1]));
System.out.print("Do you want more questions(yes/no) :");
input.nextLine();
choice = input.nextLine();
} while (choice.equalsIgnoreCase("yes"));
}
}

if (difficulty == 2) {
int num[] = new int[2];
int ans;
String choice;
do {
num = Blop();
do {
System.out.print("How much is " + num[2] + " times "
+ num[3] + " ? :");
ans = input.nextInt();
String Correct;
String Wrong;
String[] correct = { "Very good! ", "Excellent! ",
"Nice work! ", "Keep up the good work! " };
String[] wrong = { "No. Please try again. ",
"Wrong. Try once more. ", "Don’t give up! ",
"No. Keep trying " };
Random rand = new Random();
Correct = correct[rand.nextInt(correct.length)];
Wrong = wrong[rand.nextInt(wrong.length)];
if (ans == (num[2] * num[3])) {
System.out.print(Correct);
} else {
System.out.print(Wrong);
}
} while (ans != (num[2] * num[3]));
System.out.print("Do you want more questions(yes/no) :");
input.nextLine();
choice = input.nextLine();
} while (choice.equalsIgnoreCase("yes"));
}
if (difficulty == 3) {
int num[] = new int[2];
int ans;
String choice;
do {
num = Blop();
do {
System.out.print("How much is " + num[4] + " times "
+ num[5] + " ? :");
ans = input.nextInt();
String Correct;
String Wrong;
String[] correct = { "Very good! ", "Excellent! ",
"Nice work! ", "Keep up the good work! " };
String[] wrong = { "No. Please try again. ",
"Wrong. Try once more. ", "Don’t give up! ",
"No. Keep trying " };
Random rand = new Random();
Correct = correct[rand.nextInt(correct.length)];
Wrong = wrong[rand.nextInt(wrong.length)];
if (ans == (num[4] * num[5])) {
System.out.print(Correct);
} else {
System.out.print(Wrong);
}
} while (ans != (num[4] * num[5]));
System.out.print("Do you want more questions(yes/no) :");
input.nextLine();
choice = input.nextLine();
} while (choice.equalsIgnoreCase("yes"));
}
if (difficulty == 4) {
int num[] = new int[2];
int ans;
String choice;
do {
num = Blop();
do {
System.out.print("How much is " + num[6] + " times "
+ num[7] + " ? :");
ans = input.nextInt();
String Correct;
String Wrong;
String[] correct = { "Very good! ", "Excellent! ",
"Nice work! ", "Keep up the good work! " };
String[] wrong = { "No. Please try again. ",
"Wrong. Try once more. ", "Don’t give up! ",
"No. Keep trying " };
Random rand = new Random();
Correct = correct[rand.nextInt(correct.length)];
Wrong = wrong[rand.nextInt(wrong.length)];
if (ans == (num[6] * num[7])) {
System.out.print(Correct);
} else {
System.out.print(Wrong);
}
} while (ans != (num[6] * num[7]));
System.out.print("Do you want more questions(yes/no) :");
input.nextLine();
choice = input.nextLine();
} while (choice.equalsIgnoreCase("yes"));
}
System.out.print(100 / 10 * percent);
}

}

最佳答案

您的 while 循环已声明:“当总数小于或等于 10 时”

这意味着它将在 10 点再次运行。

只要完成总数<10

编辑:您也不会在任何地方增加总计。

total++;

会做的。

编辑:看来你是这么做的。抱歉,该代码在手机上很难阅读。

关于java - 结束 while 循环的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28710245/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com