gpt4 book ai didi

java - 新类(class),无法通过它们验证数学问题

转载 作者:行者123 更新时间:2023-12-02 03:12:38 25 4
gpt4 key购买 nike

我正在尝试为作业构建一个简单的数学程序。我花了一个多小时,但我似乎无法正确验证方程。其他一切似乎都有效。问题是,当需要验证问题数量是否正确时,它只计算最后一个。所以如果我答对了 3/3,它就会说 3 分之1 是对的。如果我前两个正确,第二个错误,它会说 3 中的 0 是正确的。

这是我的代码:

import java.util.Scanner;
public class ElementaryMath {

private double num1;
private double num2;
private String opType;
private int numCorrect;
private int actualAns;

ElementaryMath(double n1, double n2, String ot){
num1=n1;
num2=n2;
opType=ot;
}

public void printQuestion(){
System.out.println((int)num1 + " " + opType + " " + (int)num2);
}

public void checkAnswer(int ans, String op){

if(op.equals("/")) {
actualAns = (int) num1 / (int) num2;
System.out.println("answer is " + actualAns +" "+ ans);
}
if(op.equals("*")){
actualAns = (int) num1 * (int) num2;
System.out.println("answer is " + actualAns + " " + ans);
}

if(actualAns == ans){
numCorrect++;
}

}

public String numCorr(){
return ""+numCorrect+"";
}

public static void main(String[]args){
Scanner in = new Scanner(System.in);
double n1, n2;
String op = "";
int nq, qt, aq;

System.out.println("Which option would you like?");
System.out.println("1. Single Digit - One operation");
System.out.println("2. Two Digit - One operation.");
System.out.println("3. Single Digit - Multiple Operations (Mixed operators)");
System.out.println("");
System.out.println("Please enter your option. (1, 2, or 3): ");
qt = in.nextInt();
if(qt==1 || qt==2) {
System.out.println("Please enter which operator you would like to use: (type / or * )");
op = in.next();
}
System.out.println("Enter the number of questions you would like: ");
nq = in.nextInt();

if(qt==1 || qt==2)
{
for(int i=0;i<nq;i++) {
n1 = Math.random() * 9 + 1;
n2 = Math.random() * 9 + 1;
ElementaryMath a = new ElementaryMath(n1, n2, op);
a.printQuestion();
System.out.println("Please print your answer: ");
aq = in.nextInt();
a.checkAnswer(aq, op);
if(i==(nq-1)) {
System.out.println("You have successfully answered " + a.numCorr() + " out of " + nq + " questions correctly.");
}
}
}
}
}

最佳答案

您的问题是您需要一个静态计数器变量(numCorrect)。然后,您需要检查给出的答案是否正确。这是修改后的代码。

import java.util.Scanner;
public class ElementaryMath {

private double num1;
private double num2;
private String opType;
private static int numCorrect;
private int actualAns;

ElementaryMath(double n1, double n2, String ot){
num1=n1;
num2=n2;
opType=ot;
}

public void printQuestion(){
System.out.println((int)num1 + " " + opType + " " + (int)num2);
}

public void checkAnswer(int ans, String op){

if(op.equals("/")) {
actualAns = (int) num1 / (int) num2;
System.out.println("answer is " + actualAns +" "+ ans);
if(actualAns == ans)
{
numCorrect++;
}
}
if(op.equals("*")){
actualAns = (int) num1 * (int) num2;
System.out.println("answer is " + actualAns + " " + ans);
if(actualAns == ans)
{
numCorrect++;
}
}
}

public String numCorr(){
return ""+numCorrect+"";
}

public static void main(String[]args){
Scanner in = new Scanner(System.in);
double n1, n2;
String op = "";
int nq, qt, aq;

System.out.println("Which option would you like?");
System.out.println("1. Single Digit - One operation");
System.out.println("2. Two Digit - One operation.");
System.out.println("3. Single Digit - Multiple Operations (Mixed operators)");
System.out.println("");
System.out.println("Please enter your option. (1, 2, or 3): ");
qt = in.nextInt();
if(qt==1 || qt==2) {
System.out.println("Please enter which operator you would like to use: (type / or * )");
op = in.next();
}
System.out.println("Enter the number of questions you would like: ");
nq = in.nextInt();

if(qt==1 || qt==2)
{
for(int i=0;i<nq;i++) {
n1 = Math.random() * 9 + 1;
n2 = Math.random() * 9 + 1;
ElementaryMath a = new ElementaryMath(n1, n2, op);
a.printQuestion();
System.out.println("Please print your answer: ");
aq = in.nextInt();
a.checkAnswer(aq, op);
if(i==(nq-1)) {
System.out.println("You have successfully answered " + numCorrect + " out of " + nq + " questions correctly.");
}
}
}
}
}

这可以解决您的问题,例如计数错误。

关于java - 新类(class),无法通过它们验证数学问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40814388/

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