[英]How do I repeat this in Java?
當學生得到正確的問題時,該程序會詢問另一個隨機問題。 但是當學生提出錯誤的問題時,它仍然會問他們一個新問題。 我想改變它,以便當學生得到錯誤的問題時,它再次問他們同樣的問題,直到他們做對了。 請幫忙,非常感謝你!
import java.util.Random;
import java.util.Scanner;
public class Multiplication {
static Random random = new Random();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int correctCtr =0;
int wrongCtr = 0;
System.out.println("Welcome to the CAI-Knowledge Assessment\n ");
int type = getProblemType(scanner);
System.out.print("Enter difficulty level: ");
int difficulty = scanner.nextInt();
while (true) {
int questionAns = getQuestion(type, difficulty);
int answer = scanner.nextInt();
if (questionAns != answer) {
System.out.println(getWrongResponse());
wrongCtr++;
} else {
System.out.println(getCorrectResponse());
correctCtr++;
}
if ((wrongCtr + correctCtr) == 10) {
double correctness = (correctCtr / 10.0) * 100;
if (correctness > 75) {
System.out.println("Congratulations, you are ready to go to the next level!");
} else {
System.out.println("Please ask your teacher for extra help.");
}
wrongCtr = 0;
correctCtr = 0;
while (true) {
System.out.print("Try again? Y or any other character to exit:");
String response = scanner.next();
if (response.equalsIgnoreCase("y")) {
type = getProblemType(scanner);
System.out.print("Enter difficulty level: ");
difficulty = scanner.nextInt();
break;
} else {
System.out.println("Thank you for learning!");
System.exit(0);
}
}
}
}
}
static int getProblemType(Scanner scanner) {
int type;
while (true) {
System.out.print("What type of problem would you like? \n(1=addition, 2=subtraction, 3=multiplication, 4=division, 5=mixture): ");
type = scanner.nextInt();
if (type > 0 && type < 6) {
break;
} else {
System.out.println("Invalid type!");
}
}
return type;
}
static String getCorrectResponse() {
String response = null;
int index = random.nextInt(4);
switch (index) {
case 0:
response = "Very good!";
break;
case 1:
response = "Excellent!";
break;
case 2:
response = "Nice work!";
break;
case 3:
response = "Keep up the good work!";
break;
}
return response;
}
static String getWrongResponse() {
String response = null;
int index = random.nextInt(4);
switch (index) {
case 0:
response = "No. Please try again.";
break;
case 1:
response = "Wrong. Try once more.";
break;
case 2:
response = "Don't give up!";
break;
case 3:
response = "No. Keep trying.";
break;
}
return response;
}
static int getQuestion(int type, int difficulty) {
int no1 = random.nextInt((int)Math.pow(10, difficulty));
int no2 = random.nextInt((int)Math.pow(10, difficulty));
int returnVal = 0;
if (type == 1) {
System.out.printf("How much is %d plus %d? ",no1,no2);
returnVal = no1 + no2;
} else if (type == 2) {
while (no2 > no1) {
no2 = random.nextInt((int)Math.pow(10, difficulty));
}
System.out.printf("How much is %d minus %d? ",no1,no2);
returnVal = no1 - no2;
} else if (type == 3) {
System.out.printf("How much is %d times %d? ",no1,no2);
returnVal = no1 * no2;
} else if (type == 4) {
while (no2 == 0 || no2 > no1 || no1%no2!=0) {
no2 = random.nextInt((int)Math.pow(10, difficulty));
}
System.out.printf("How much is %d divided by %d? ",no1,no2);
returnVal = no1 * no2;
} else if (type == 5) {
returnVal = getQuestion(random.nextInt(5)+1, difficulty);
}
return returnVal;
}
}
你自己說的
Set<String> questions = getAllQuestions();
for( String question : questions) {
boolean correctAnswer = false;
while(!correctAnswer) {
correctAnswer = askQuestion(question);
}
}
現在,只需填寫空白即可。 在模板算法中實現方法。 如果此人不知道正確答案,為避免遇到無限循環,您可能希望在經過一定次數的嘗試后中斷循環。 我相信你現在可以很容易地將這個邏輯添加到模板算法中。
你可以采取多種方式。 一個簡單的hack可以在所示的相同點迭代 -
int answer = scanner.nextInt();
if (questionAns != answer) {
*while (questionAns != answer)*{
wrongAnswer();
System.out.println("Try Again");
int answer = scanner.nextInt();
}
}
static void wrongAnswer(){
System.out.println(getWrongResponse());
wrongCtr++;
}
PS我沒有測試過代碼。 祝好運。
只需設置另一個變量來跟蹤它們是否正確的問題,然后只有在該變量為false時重置問題。
Boolean questionCorrect = true;
while (true) {
if (questionCorrect) {
int questionAns = getQuestion(type, difficulty);
}
int answer = scanner.nextInt();
if (questionAns != answer) {
System.out.println(getWrongResponse());
wrongCtr++;
questionCorrect = false;
} else {
System.out.println(getCorrectResponse());
correctCtr++;
questionCorrect = true;
}
隨機生成no1
, no2
。 所以,你應該把你的問題保存在全局變量中。 在getQuestion(int type, int difficulty)
方法中分配變量。 這是示例代碼。
String question=""
static int getQuestion(int type, int difficulty){
if(type==2){
question=String.format("How much is %d minus %d? ",no1,no2);
}
}
如果用戶出錯,你可以問同樣的問題。
你應該簡單地緩存一個questionAns(你的int)的實例和一個字符串變量,它在初始化時的格式與打印輸出的格式相同(你可以通過String.format()來實現)。
一旦完成此操作,您還可以包含一個臨時布爾值,該布爾值根據用戶提供的正確或不正確的值進行更改。 然后在while循環的下一次迭代中讀取此布爾值,然后讓您選擇在屏幕上打印新問題(通過您的方法)或打印保存的格式化字符串,這是上一個問題。
添加2個新變量
boolean isTrue = true;
boolean isFalse = true;
並編輯此部分
if (isTrue)
int questionAns = getQuestion(type, difficulty);
int answer = scanner.nextInt();
if (questionAns != answer) {
System.out.println(getWrongResponse());
wrongCtr++;
isTrue = false;
isFalse = true;
} else {
System.out.println(getCorrectResponse());
correctCtr++;
isTrue = true;
isFalse = false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.