Java 从另一个类调用类时尝试获取新值

Java 从另一个类调用类时尝试获取新值,java,Java,我正在用java编写一个数学测验题,基本上问10个问题,给用户2个答案 找到正确答案的机会。该程序根据基本算法提问。此外, 减法、除法和乘法。 我的问题是,当我运行程序时,它运行得很好,但在程序的后面,它会问同样的问题 具有相同值的问题。如何让程序每次都使用新值 这个女孩叫什么? 我现在把加、减、乘、除的课程都忘了。如果你需要的话,请告诉我 知道 import java.util.Scanner; import java.math.*; public class MathQ { pub

我正在用java编写一个数学测验题,基本上问10个问题,给用户2个答案 找到正确答案的机会。该程序根据基本算法提问。此外, 减法、除法和乘法。 我的问题是,当我运行程序时,它运行得很好,但在程序的后面,它会问同样的问题 具有相同值的问题。如何让程序每次都使用新值 这个女孩叫什么? 我现在把加、减、乘、除的课程都忘了。如果你需要的话,请告诉我 知道

 import java.util.Scanner;
 import java.math.*;


public class MathQ {
 public static void main(String[] args) {
    int credit = 0;
    int firstTry = 0;
    int secondTry = 0;
    int wrong = 0;
    System.out.print("What's your name?");
    Scanner sc = new Scanner(System.in);
    String name = sc.next();
    AdditionProblem q1 = new AdditionProblem();
    SubtractionProblem q2 = new SubtractionProblem();
    MultiplicationProblem q3 = new MultiplicationProblem();
    DivisionProblem q4 = new DivisionProblem();

    System.out.println("Nice to meet you , " + name + "!");
    System.out.println("**************MATH CHALLENGE*************");
    System.out.println(
            "This challenge will present you 10 problems in which you will have two chances to answer 
 the question correctly");
    System.out.println(
            "The questions that will be asked will be basic arithmetic ranging from addition, 
 subtraction, multiplication and division");
    System.out.println("You will receive full credit if you answer the question correctly on the 
 first try");
    System.out.println("You will only receive half credit if you answer it correctly on the second 
 try");
    System.out.println("You score will be tallied at the end of the quiz");
    System.out.println("Good luck!");
    System.out.println("---------------------");

    for (int i = 1; i <= 10; i++) {
        long rand1 = Math.round((Math.random() * 4) + 1);
        // System.out.println(rand1); //for testing random function

        if (rand1 == 1.0) {
            System.out.println("Problem #" + i);
            System.out.println("----------------------------");
            System.out.println(q1.getProblem());

            System.out.println("Type Your Answer Below:");
            Integer ans = sc.nextInt();
            System.out.println("Your answer was: " + ans);
            if (ans == q1.getAnswer()) {
                System.out.println("That is correct!");
                credit = credit + 10;
                firstTry = firstTry + 1;
                System.out.println("Your total points right now is: " + credit);
                System.out.println();
            } else {
                System.out.println("-------------------------");
                System.out.println();
                System.out.println("Sorry that is not correct");
                System.out.println("Lets try again");
                System.out.println();
                System.out.println(q1.getProblem());
                System.out.println("Type Your Answer:");
                Integer ans1 = sc.nextInt();
                if (ans1 == q1.getAnswer()) {
                    System.out.println("There ya go..good answer");
                    System.out.println("Lets move on");
                    credit = credit + 5;
                    secondTry = secondTry + 1;
                    System.out.println("You total points thus far is: " + credit);
                } else {
                    System.out.println();
                    System.out.println("Hmmmm.sorry thats still not correct");
                    System.out.println("The correct answer was: " + q1.getAnswer());
                    wrong = wrong + 1;
                    System.out.println("You have " + credit + " points");
                    System.out.println("Lets continue on!");
                    System.out.println();
                    System.out.println("-------------------------");
                }

            }
import java.util.Scanner;
导入java.math.*;
公共类数学{
公共静态void main(字符串[]args){
积分=0;
int firstTry=0;
int secondTry=0;
int错误=0;
系统输出打印(“你叫什么名字?”);
扫描仪sc=新的扫描仪(System.in);
字符串名称=sc.next();
AdditionProblem q1=新的AdditionProblem();
减法题q2=新的减法题();
乘法问题q3=新乘法问题();
分区问题q4=新分区问题();
System.out.println(“很高兴认识你,“+name+”!”;
System.out.println(“****************数学挑战*******************”;
System.out.println(
“这项挑战将向你提出10个问题,你将有两次机会回答
问题是正确的);
System.out.println(
“将要问的问题是基本的算术,包括加法,
减法、乘法和除法);
System.out.println(“如果您正确回答了问题,您将获得满分
第一次尝试);
System.out.println(“如果你在第二天回答正确,你将只获得一半学分
尝试);
System.out.println(“您的分数将在测验结束时统计”);
System.out.println(“祝你好运!”);
System.out.println(“--------------------------”;

对于(int i=1;i我假设您发布的psvm包含实际代码。如果不是这样,请为我填补空白

代码示例有多个问题,我将逐一解决

您有4个本地属性q1-q4和4个不同的实例。根据代码,我看到每个类包含一个问题和一个答案。这已经限制了您最多4个问题。您无法获得10个不同的问题

为了解决这个问题,你必须重新考虑你的方法。我现在不会在OO方面走极端,因为这似乎不是你的目标。 首先,我们必须修复您的“静态”局部变量,并添加一些实用程序来启用动态问题我们可以从主类中删除此分离。因此,我们添加了一个新类,我们称之为ProblemFactory,以及一个接口,我们称之为Problem。您的4个类实现了Problem

公共最终类问题工厂{
公共静态问题createProblem(){
long random=Math.round((Math.random()*4)+1);
开关(随机){
案例1:
返回新的AdditionProblem();
案例2:
返回新问题();
...
}
}
公共接口问题{
公共字符串getProblem();
公共字符串getAnswer();
}
在主类中,将Math.random()行替换为

Problem-Problem=ProblemFactory.createProblem();
您不再需要在主类中使用if(rand1==1.0),您可以删除除其中一个之外的所有if-else块。q1-q4将替换为新的问题变量

现在,您需要对您的4个问题类进行最后一次更改。我假设您有某种类型的映射,其中包含2个字符串,其中key是问题,value是答案。如果是这种情况,您的类可能如下所示


公共类AdditionProblem(){
私有静态映射问题=createQuestions();
私有字符串问题;
私有字符串应答;
私有静态映射createQuestions(){
映射问题=新HashMap();
问题。付诸表决(“24+36+10”、“70”);
//增加你想要的数量,但总共至少10个
回答问题;
}
公共附加问题(){
//我们首先得到可用问题的数量
int numberOfQuestions=questions.size();
//我们现在再次生成一个随机变量来选择要选择的问题
long random=Math.round((Math.random()*numberOfQuestions)+1);
//我们从地图中提取他们的密钥集,并使用随机数作为索引,这是我们现在的问题
problem=questions.keys().get(随机);
//我们用这个问题得出答案
答案=问题。获取(问题);
//作为我们的最后一步,我们将问题从列表中删除,这样就不会再次提取该问题
问题。删除(问题);
}
公共字符串getProblem(){
退货问题;
}
公共字符串getAnswer(){
返回答案;
}
}
通过这些更改,您在每次循环迭代中都会创建一个新的问题对象。您不知道或不关心它是什么类型的算术函数,您只知道它是4个问题中的一个,并且总是一个新问题。
你现在唯一需要确保的是,当你想在测验中有10个问题时,每堂课至少有10个问题。否则,你最终会遇到异常。

考虑将你的代码拆分为方法,每个方法都会进行逻辑运算,例如getInput或Calculate,我非常感谢你!我已经学会了在阅读你的评论时,你会意识到你是多么的简单和合乎逻辑