gpt4 book ai didi

java - 多个方法的全局Int变量方法

转载 作者:太空宇宙 更新时间:2023-11-04 14:33:44 26 4
gpt4 key购买 nike

我必须建立一个数学程序,根据用户的年级水平向他们提供随机问题,如果他们做得好,就提高难度。我在这里设置了运行问题的代码,如果它们满足要求,它会显示更难的问题,但是如果我用“i”来确定他们如何完成成员(member)问题,如果分开,程序将运行更难的问题然后回去完成更简单的问题

所以基本上我尝试为全局“i”编写一个所有其他方法都会使用的方法,但是当我用该方法替换“i”时,它会停止计数并继续无限地显示问题,我不知道如何解决这个问题。

import java.util.Scanner; 
import java.util.*;
import java.util.Date;

public class Quiz {

public static void main(String[] args) {

int answer;
int correct;
double current_score = 100.00;
// int i = 0;

while (questionsDone() < 10) { // start of question loop

int random = (int) (Math.random() * 9 + 0);
int random2 = (int) (Math.random() * 9 + 0);

System.out.print("What is the sum of" + " ");
System.out.print(random);
System.out.print(" + " + random2 + " ");
System.out.print("=" + " ");

Scanner scan = new Scanner(System.in);
//answer
answer = scan.nextInt();

correct = random + random2;

if (answer == correct) { // start of result display
System.out.println("You are correct");
} else if (answer != correct) {
System.out.println("That wasn't right");
current_score = (current_score - 10.00);
}

System.out.println("Your current percentage is " + current_score); // end of result display

// i++; // raise number of questions given by 1
if (questionsDone() == 5 && current_score >= 75) { // code to move up or down year level
System.out.println("You are doing well! Let's increase the difficulty a little");
Year1_10Questions();
}

}

}

public static void Year1_10Questions() {

int i = 0;
int answer;
int correct;
double current_score = 100.00;
while (i < 10) { // start of question loop

int random = (int) (Math.random() * 9 + 0);
int random2 = (int) (Math.random() * 9 + 0);
int random3 = (int) (Math.random() * 2 + 1);

String operator = "";
switch (random3) {
case 1:
operator = "+";
break;
case 2:
operator = "-";
break;
}

System.out.print("What is the sum of ");
System.out.print(" " + random + " ");
System.out.print(operator + " ");
System.out.print(random2 + " ");

Scanner scan = new Scanner(System.in);
//answer
answer = scan.nextInt();

if (random3 == 1) {
correct = random + random2;

if (answer == correct) { // start of result display
System.out.println("You are correct");
} else if (answer != correct) {
System.out.println("That wasn't right");
current_score = (current_score - 10);

}
} else if (random3 == 2) {
correct = random - random2;

if (answer == correct) { // start of result display
System.out.println("You are correct");
} else if (answer != correct) {
System.out.println("That wasn't right");
current_score = (current_score - 10);

}

}
System.out.println("Your current percentage is " + current_score); // end of result display

i++; // raise number of questions given by 1

}

} // end of year 1_10 questions

public static int questionsDone() {
int i = 0;
i++;
return i;
}

}

最佳答案

由于所有方法都在同一个类中,因此您可以在类级别定义 i:

public class Quiz {
static int i;
...
}

关于java - 多个方法的全局Int变量方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25831314/

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