Java 数学游戏-返回点的方法
因此,我的目标是创建一个数学游戏,用户可以从文件中选择一个数学问题,或者随机生成一个由3个困难中的4个数学元素组成的问题。我创建了很多方法。。。我知道我要去哪里,但现在我卡住了。我需要它,这样它就可以保留正确回答的问题的分数。在用户按gamePlaymethod上的3之前,我如何将点数返回到main方法并让游戏继续进行 公共类MathGameProject2{Java 数学游戏-返回点的方法,java,random,methods,Java,Random,Methods,因此,我的目标是创建一个数学游戏,用户可以从文件中选择一个数学问题,或者随机生成一个由3个困难中的4个数学元素组成的问题。我创建了很多方法。。。我知道我要去哪里,但现在我卡住了。我需要它,这样它就可以保留正确回答的问题的分数。在用户按gamePlaymethod上的3之前,我如何将点数返回到main方法并让游戏继续进行 公共类MathGameProject2{ public static void main(String[] args) { Scanner keyboard = new
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int score;
int points = 0;
int questionType;
System.out.print("Please enter the what type of question you want" + "\n 1 Question from a file" + "\n 2 Random question" + "\n 3 Quit game\n");
questionType = keyboard.nextInt();
while (questionType != 3) {
if (questionType == 1) {
questionFromFile();
} else if (questionType == 2) {
randomQuestion();
} else {
System.out.println("Please enter the what type of question you want" + "\n 1 Question from a file" + "\n 2 Random question" + "\n 3 Quit game\n");
}
}
}
public static questionFromFile() {
}
public static randomQuestion() {
Scanner keyboard = new Scanner(System.in);
int difficulty;
System.out.println("Please enter the difficulty you want to play." + "\n 1. Easy" + "\n 2. Medium" + "\n 3. Hard\n");
difficulty = keyboard.nextInt();
if (difficulty == 1) {
easy();
} else if (difficulty == 2) {
medium();
} else if (difficulty == 3) {
hard();
} else {
System.out.println("Please enter a number between 1-3\n");
}
}
public static easy() {
Scanner keyboard = new Scanner(System.in);
int mathElement;
System.out.print("What element of maths do you want?" + "\n1 Additon" + "\n2 Subtraction" + "\n3 Multiplication" + "\n4 Division\n");
mathElement = keyboard.nextInt();
if (mathElement == 1) {
easyAdd();
} else if (mathElement == 2) {
easySub();
} else if (mathElement == 3) {
easyMulti();
} else if (mathElement == 4) {
easyDiv();
} else {
System.out.println("Please enter a number between 1-4\n");
}
}
public static easyAdd() {
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
int num = rand.nextInt(10) + 1;
int num2 = rand.nextInt(10) + 1;
int correct = num + num2;
int answer;
System.out.print("What is the answer of " + num + " + " + num2 + " ?");
answer = keyboard.nextInt();
if (answer == correct) {
}
}
为了跟踪用户成功回答的问题数量,您需要: 对于每个问题,返回用户是否正确回答 有一个计数器,每当用户正确回答问题时,计数器就会递增 或者,有一个计数器,每当问题回答错误时,计数器就会递增 对于1,可以使用布尔返回值指定问题是否已成功回答
return (answer == correct);
您将希望将该返回值一直传播到main方法
static void main() {
....
boolean isCorrect = randomQuestion();
....
}
static boolean randomQuestion() {
....
return easy();
....
}
static boolean easy() {
....
return easyAdd();
....
}
static boolean easyAdd() {
...
return (answer == correct);
}
然后,对于2和3,您可以根据randomQuestion返回的值来增加main中定义的计数器
此外,没有双关语,您可以使用while循环持续接收用户输入,直到您获得退出代码(在本例中为3)。一种方法是使用whiletrue循环,并在用户输入3时中断
while (true) {
/* Get user input */
....
if (questionType == 3) {
break;
}
}
最后,在循环之后,您可以简单地打印出numberCorrect和numberIncorrect计数器的值
希望这能有所帮助。这段代码不会编译。方法必须有返回类型。这正是允许它们向调用方方法返回某些内容的原因。阅读,我知道,这并不是因为我被卡住而完成的。如果你是新手,你不应该在不编译和运行它的情况下键入那么多代码。一步一步地进行。编译在你添加到代码中的每一行之后。你的代码应该一直编译。我只是键入了结构,然后编写了实际的游戏,我绕错了方向,但它贯穿了所有的问题。我只是没有返回值,也不知道如何让积分系统工作以及如何运行代码,直到用户想要停止。你会发现周围有很多人这里非常重视提问者提供的信息……这有助于我们找出真正的错误,因为我们很少能仅仅通过查看代码就立即发现错误,这表明您确实首先做出了一些努力,因为这不是让您完成工作的地方。如果您提供代码,您将得到最好的答案at编译并运行,并且可以作为独立程序运行。
while (true) {
/* Get user input */
....
if (questionType == 3) {
break;
}
}