[英]Create a simple math game using java objects
我正在嘗試使用Java對象創建一個簡單的數學游戲。 我的目標是創建一個問題類,其中包含一個使用隨機數顯示問題的方法,一種檢查答案的方法以及一個問題結構。 如何使用這些對象生成10個隨機問題? 帶有加法,減法和乘法問題。 並在最后打印多少個正確答案。
我已經創建了問題類,並且我的第一種方法使用變量“ a”和“ b”顯示了一個隨機問題。 並存儲答案。 我的第二種方法用用戶輸入檢查答案並打印結果。 到目前為止,當我運行程序時,它只會一遍又一遍地顯示相同的問題。
這是我的課題班
import java.util.*;
public class Question {
private int a;
private int b;
private String Question;
private int correct;
Random rand = new Random();
Scanner input = new Scanner(System.in);
int count = 0;
Question(int c, int d) {
x = rand.nextInt(20);
y = rand.nextInt(20);
}
public void askQuestion() {
if (a > b) {
System.out.println("What is " + a + " - " + b + " ?\n");
correct = a - b;
} else {
System.out.println("What is " + a + " + " + b + " ?\n");
correct = a + b;
}
}
public void Check() {
int response = Integer.parseInt(input.next());
if (response == correct) {
System.out.printf("yes.\n");
count++;
} else {
System.out.printf("No. It is " + correct + ".\n");
}
}
}
我的主要方法是這樣的
public class Main {
public static void main(String[] args) {
Question q1 = new Question(1,2);
for (int i = 1; i < 10; i++) {
q1.askQuestion();
q1.check();
}
}
}
在我的輸出中,它顯示了帶有兩個隨機數的問題,但是它一次又一次地打印相同的問題。 例如:
What is 13 - 1 ?
12
That is correct.
What is 13 - 1 ?
12
That is correct.
What is 13 - 1 ?
3
Wrong!. The answer is 12.
What is 13 - 1 ?
最終,我希望輸出如下所示:
What is 4 + 6?
What is 7 - 3?
對解決此問題有幫助嗎? 並使游戲更具互動性? 欣賞它。
您的問題是由於您正在創建一個 Question對象,該對象會生成兩個隨機數(在您的情況下為13和1)。 然后,您將經歷一個詢問10個問題的循環,但是您使用相同的Question對象-因此每次都使用相同的隨機數。 要解決此問題,請進行以下更改:
在您的Question構造函數中,擺脫掉參數,您不需要它們。 分配給變量a
和b
:
private Question(){
a = rand.nextInt(20);
b = rand.nextInt(20);
}
因此,每次創建一個Question時,都會生成兩個隨機數,這些隨機數分配給您在頂部代碼中先前聲明的變量(在代碼中, a
和b
被聲明,但未使用)。
然后在您的主目錄中,將其更改為以下內容:
public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
Question q1 = new Question();
q1.askQuestion();
q1.check();
}
System.out.println("Number correct: " + count); //print amount correct, to use this make variable "count" static.
}
所做的更改是,現在您每次循環時都創建一個新的Question對象,並獲得新的隨機值。 每次詢問新問題時,都會創建一個具有新隨機值的新Question對象,並覆蓋舊值。 給出並檢查答案后,它將詢問10次,然后程序將輸出正確答案的數量並停止。
3個問題的示例輸出:
What is 17 - 15 ?
2
yes.
What is 8 + 11 ?
19
yes.
What is 9 - 0 ?
5
No. It is 9.
Number correct: 2
如果您想要一種用隨機數和運算符動態詢問問題的方法,則可以創建一個如下所示的Operator
枚舉,以處理左手值和右手值的結果。
另外,對System.out.print
的調用應盡可能在主程序中。 相反,您應該從Question
返回字符串。
您需要做的就是將兩個隨機生成的數字傳遞給Operator枚舉,並要求其計算結果。
package exam;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Exam {
private static int correctCount = 0;
private static List<Question> questions = randomQuestions(10);
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
questions.stream().forEach(question -> ask(question, input));
input.close();
stats();
}
private static void ask(Question question, Scanner input) {
System.out.print(question.askQuestion());
double guess = input.nextDouble();
boolean isCorrect = question.makeGuess(guess);
System.out.println(question.explain(isCorrect));
System.out.println();
correctCount += isCorrect ? 1 : 0;
}
private static void stats() {
double percentage = (correctCount * 1.0d) / questions.size() * 100;
System.out.printf("Correct: %.2f%% (%d/%d)%n", percentage, correctCount, questions.size());
}
private static List<Question> randomQuestions(int count) {
List<Question> questions = new ArrayList<Question>();
while (count --> 0) questions.add(new Question());
return questions;
}
}
package exam;
import java.util.Random;
public class Question {
private static final Random RAND = new Random(System.currentTimeMillis());
private double left;
private double right;
private Operator operator;
public Question(double left, double right, Operator operator) {
this.left = left;
this.right = right;
this.operator = operator;
}
public Question(int max) {
this(randInt(max), randInt(max), Operator.randomOperator());
}
public Question() {
this(10); // Random 0 -> 10
}
public String askQuestion() {
return String.format("What is %s? ", operator.expression(left, right));
}
public String explain(boolean correct) {
return correct ? "Correct" : String.format("Incorrect, it is: %.2f", calculate());
}
public boolean makeGuess(double guess) {
return compareDouble(guess, calculate(), 0.01);
}
private double calculate() {
return operator.calculate(left, right);
}
@Override
public String toString() {
return String.format("%s = %.2f", operator.expression(left, right), calculate());
}
private static boolean compareDouble(double expected, double actual, double threshold) {
return Math.abs(expected - actual) < threshold;
}
private static double randInt(int range) {
return Math.floor(RAND.nextDouble() * range);
}
}
package exam;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public enum Operator {
ADD("+", (left, right) -> left + right),
SUB("-", (left, right) -> left - right),
MUL("*", (left, right) -> left * right),
DIV("/", (left, right) -> left / right);
private static final Random RAND = new Random(System.currentTimeMillis());
private static final List<Operator> VALUES = Collections.unmodifiableList(Arrays.asList(values()));
private static final int SIZE = VALUES.size();
public static Operator randomOperator() {
return VALUES.get(RAND.nextInt(SIZE));
}
private String symbol;
private Operation operation;
private Operator(String symbol, Operation operation) {
this.symbol = symbol;
this.operation = operation;
}
public double calculate(double left, double right) {
return operation.calculate(left, right);
}
public String expression(double left, double right) {
return String.format("%.2f %s %.2f", left, symbol, right);
}
@Override
public String toString() {
return symbol;
}
}
package exam;
public interface Operation {
double calculate(double left, double right);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.