Java 在一个问题连续正确回答3次/并添加差异后,我如何将程序循环回开始

Java 在一个问题连续正确回答3次/并添加差异后,我如何将程序循环回开始,java,Java,我正在创建一个程序来帮助学生解决y=m(x)+b。现在,我有一个程序来显示菜单并评估你的回答是否正确。但是,我需要它来计算一行中正确答案的数量 如果3次正确,则结束程序并输出总正确尝试次数 否则,如果有3次尝试,则输出为提示 我遇到的主要问题是这两种方法的循环。如果我的代码糟糕透顶,我要提前道歉。与Python相比,我很难理解其中的方法和类。任何人的建议或提示都会非常有用 到目前为止,我已经尝试添加方法,并尝试将类添加到程序的某些部分,例如 publicstaticvoid用户输入(线交叉的

我正在创建一个程序来帮助学生解决y=m(x)+b。现在,我有一个程序来显示菜单并评估你的回答是否正确。但是,我需要它来计算一行中正确答案的数量

  • 如果3次正确,则结束程序并输出总正确尝试次数
  • 否则,如果有3次尝试,则输出为提示
我遇到的主要问题是这两种方法的循环。如果我的代码糟糕透顶,我要提前道歉。与Python相比,我很难理解其中的方法和类。任何人的建议或提示都会非常有用

到目前为止,我已经尝试添加方法,并尝试将类添加到程序的某些部分,例如

publicstaticvoid用户输入(线交叉的int点,int斜率,int y截距,int独立变量){}

public static test_input(){}

然而,现在我面临范围界定问题以及引用某些变量的错误

包代数\u导师;
导入java.util.Scanner;
类代数算子{
公共静态void main(字符串[]args){
System.out.println(“如果您想求解Y,请输入1?”);
System.out.println(“如果您想求解M,请输入2?”);
System.out.println(“如果您想求解B,请输入3?”);
System.out.println(“输入4退出”);
//请求用户输入
System.out.print(“输入您的选择:”);
}
//为公式中的值创建随机#
int y=线交叉点;
int m=斜率;
int b_u=y_截距;
int x_u=独立变量;
publicstaticvoid用户输入(线交叉的int点、int斜率、int y截距、int独立变量){
//创建作为菜单选择器输入菜单定义的扫描仪
扫描仪用户_选择=新扫描仪(System.in);
//将用户输入转换为整数
int selection=user_selection.nextInt();
用户_选择。关闭();
y_截距=(int)(数学地板(数学随机()*201)-100);
斜率=(int)数学地板(Math.random()*201)-100;
线交叉的点=(int)Math.floor(Math.random()*201)-100;
自变量=(int)Math.floor(Math.random()*201)-100;
//测试用户输入是什么,以及预期的输出
如果(选择==(1)){
System.out.println(“您选择为Y求解:”;
System.out.println(“Y=“+slope+”(“+independent_variable+”)”+“+”+Y_intercept);
System.out.println(“输入您的答案:”);
}
else if(选择==(2)){
System.out.println(“您选择为M求解:”;
System.out.println(“M=“+”(“+U线的点\交叉+”-“+y \截距+”)”+“/”+独立\变量);
System.out.println(“输入您的答案:”);
}
否则如果(选择==(3)){
System.out.println(“您选择为B求解:”;
System.out.println(“B=“+直线交叉点+”-“+斜率+”(“+独立变量+”));
System.out.println(“输入您的答案:”);
}
else if(选择==(4)){
System.out.println(“您选择退出该程序。”);
返回;
}
}
//为了与用户输入进行比较,解决了这个问题
int答案y=((m)*(x))+(b);
int答案μm=(y)-(b)/(x));
int答案b=(y)-(m)*(x));
公共静态测试_输入(){
//问题求解器定义
扫描仪应答输入=新扫描仪(System.in);
int answer=answer_input.nextInt();
//为程序创建循环
var计数器=0;
int correct=0;
var回答正确=错误;
而(!答案正确&&correct<3){
如果(答案==答案y){
计数器++;
正确的++;
System.out.println(“您回答正确”);
返回;
}
否则如果(计数器>=3&&correct<3){
System.out.println(“你最近错过了问题,让我来帮你吧!”);
}
其他的
{
System.out.println(“重试”);
计数器++;
正确=0;
打破
}
}
}
}

我希望在用户连续完成3个问题后,程序能够输出正确的答案。此外,它需要在3次尝试后输出提示。然后在3正确后,它应该循环回到程序的开头

周六我做代数已经很晚了,所以我会坚持建议修改你的程序结构。首先,您可以通过一个包含问题的类来完成所有工作,并为用户评分。该类中的方法可以通过主菜单中的菜单进行选择。 我编写了一个基于标准JavaOOP方法的示例,说明了我将如何构建这个框架。在我的程序中,main不需要静态类,它循环一个菜单,并在那里选择一个问题。我的方法只有一个问题,你可以在菜单中随意添加,重要的是结构

 import java.util.Scanner;

//This class contains the two methods and over-all score
class Material {
private int score;
//The user chooses this or the earth method
public void sky() {

    String answer = "null"; 
    int count = 0;
    Scanner input = new Scanner(System.in);
   //within the method, is this while loop which gives a hint after 3 attempts.
    while (!answer.equals("blue") && (!answer.equals("exit"))) {
        System.out.println("What color is the sky? 'exit' to exit");
        answer = input.nextLine();
        count++;
        if (count == 3)
            System.out.println("Hint: It starts with a 'b'");
    }

    if (answer.equals("blue"))
        score += 1;//The score will increment if the choice is correct,
    else//or else leave with nothing...
        return;
}

    //This method is the same as the sky() method, just different question and answer.
public void earth() {

    String answer = "null";
    int count = 0;
    Scanner input = new Scanner(System.in);

    while (!answer.equals("iron") && (!answer.equals("exit"))) {
        System.out.println("What is the core made of? 'exit' to exit");
        answer = input.nextLine();
        count++;
        if (count == 3)
            System.out.println("Hint: It starts with a 'i'");
    }

    if (answer.equals("iron"))
        score += 1;
    else
        return;

}

public int getScore() {
    return score;
}

}

public class Questions {

public static void main(String[] args) {
    //No static methods needed, here is an instance of our test materia class.
    Material material = new Material();

    //The choice and scanner are instantiated here.
    int choice = 0;
    Scanner input = new Scanner(System.in);

    //This while loop uses a switch statement to choose the methods for the questions
    while (choice != 3) {

        if (material.getScore() == 3) {
            System.out.println("Good job, you scored three right.");
            return;
        }

        System.out.println("SCORE: " + material.getScore());
        System.out.println("Anwer questions about the sky: 1");
        System.out.println("Answer quetions about the earth: 2");
        System.out.println("Exit: 3");
        choice = input.nextInt();
       //choices are 1 , 2 for questions, and 3 to leave.
        switch (choice) {
        case 1:
            material.sky();
            break;
        case 2:
            material.earth();
            break;
        case 3:
            System.out.println("Exiting...");
            break;
        default:
            System.out.println("not a valid number choice...");

        }
    }

}// main
}// class

周六我做代数已经很晚了,所以我会坚持建议修改你的程序结构。首先,您可以通过一个包含问题的类来完成所有工作,并为用户评分。该类中的方法可以通过主菜单中的菜单进行选择。 我编写了一个基于标准JavaOOP方法的示例,说明了我将如何构建这个框架。在我的程序中,main不需要静态类,它循环一个菜单,并在那里选择一个问题。我的方法只有一个问题,你可以在菜单中随意添加,重要的是结构

 import java.util.Scanner;

//This class contains the two methods and over-all score
class Material {
private int score;
//The user chooses this or the earth method
public void sky() {

    String answer = "null"; 
    int count = 0;
    Scanner input = new Scanner(System.in);
   //within the method, is this while loop which gives a hint after 3 attempts.
    while (!answer.equals("blue") && (!answer.equals("exit"))) {
        System.out.println("What color is the sky? 'exit' to exit");
        answer = input.nextLine();
        count++;
        if (count == 3)
            System.out.println("Hint: It starts with a 'b'");
    }

    if (answer.equals("blue"))
        score += 1;//The score will increment if the choice is correct,
    else//or else leave with nothing...
        return;
}

    //This method is the same as the sky() method, just different question and answer.
public void earth() {

    String answer = "null";
    int count = 0;
    Scanner input = new Scanner(System.in);

    while (!answer.equals("iron") && (!answer.equals("exit"))) {
        System.out.println("What is the core made of? 'exit' to exit");
        answer = input.nextLine();
        count++;
        if (count == 3)
            System.out.println("Hint: It starts with a 'i'");
    }

    if (answer.equals("iron"))
        score += 1;
    else
        return;

}

public int getScore() {
    return score;
}

}

public class Questions {

public static void main(String[] args) {
    //No static methods needed, here is an instance of our test materia class.
    Material material = new Material();

    //The choice and scanner are instantiated here.
    int choice = 0;
    Scanner input = new Scanner(System.in);

    //This while loop uses a switch statement to choose the methods for the questions
    while (choice != 3) {

        if (material.getScore() == 3) {
            System.out.println("Good job, you scored three right.");
            return;
        }

        System.out.println("SCORE: " + material.getScore());
        System.out.println("Anwer questions about the sky: 1");
        System.out.println("Answer quetions about the earth: 2");
        System.out.println("Exit: 3");
        choice = input.nextInt();
       //choices are 1 , 2 for questions, and 3 to leave.
        switch (choice) {
        case 1:
            material.sky();
            break;
        case 2:
            material.earth();
            break;
        case 3:
            System.out.println("Exiting...");
            break;
        default:
            System.out.println("not a valid number choice...");

        }
    }

}// main
}// class

好吧,我想我会让你自己想出如何让它循环,但我解决了你的其他问题,并把评论放在我改变事情的地方。希望t
public static void user_input(int point_of_line_cross, int slope, int y_intercept, int independent_variable)
y_intercept = (int) (Math.floor(Math.random() * 201) - 100);
slope = (int) Math.floor(Math.random() * 201) - 100;
point_of_line_cross = (int) Math.floor(Math.random() * 201) - 100;
independent_variable = (int) Math.floor(Math.random() * 201) - 100;
Scanner answer_input = new Scanner(System.in);
int answer = answer_input.nextInt();
var counter = 0;
int counter = 0;
public class AlgebraTutor {

    private final Scanner in = new Scanner(System.in);
    private final PrintStream out = System.out;

    private int attempts = 0;

    void solveForY() {
        ...
    }

    void solveForM() {
        ...
    }

    void solveForB() {
        ...
    }

    void mainMenu() {
        while (true) {
            out.println("Solve the equation y = m * x + b for which variable (y, m, b), or quit?");
            if (!in.hasNextLine()) {
                return;
            }

            switch (in.nextLine()) {
            case "y":
                solveForY();
                break;

            case "m":
                solveForX();
                break;

            case "b":
                solveForB();
                break;

            case "q":
            case "quit":
                return;
            }
        }
    }

    public static void main(String[] args) {
        new AlgebraTutor().mainLoop();
    }
}