首页 > 解决方案 > 在一个问题连续正确回答 3 次后,我将如何将我的程序循环回到开头/并添加方差

问题描述

我正在创建一个程序来帮助学生解决 y= m(x) + b。截至目前,我有程序可以显示菜单并评估您的回答是否正确。但是,我还需要它来计算连续正确答案的数量。

我遇到的主要问题是两者的循环(方法?)。如果我的代码很糟糕,我提前道歉,与 Python 相比,我很难理解其中的方法和类。任何人的建议或提示都会非常有帮助。

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

public static void user_input(int point_of_line_cross, int slope, int y_intercept, int independent_variable) {}

public static test_input() {}

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

package algebra_Tutor;
import java.util.Scanner;
class AlgebraTutor {

public static void main(String[] args){
    System.out.println("Enter 1 if you would like to solve for Y?");
    System.out.println("Enter 2 if you would like to solve for M?");
    System.out.println("Enter 3 if you would like to solve for B?");
    System.out.println("Enter 4 to Quit");


    //Asks for user input
    System.out.print("Enter your selection: ");
    }

    //Creates random # for values in formula
    int y_ = point_of_line_cross;
    int m_ = slope;
    int b_ = y_intercept; 
    int x_ = independent_variable;

public static void user_input(int point_of_line_cross, int slope, int y_intercept, int independent_variable) {

            // Creates scanner for input of menu Def as menu selector
    Scanner user_Selection = new Scanner(System.in);

            //Converts user input to an integer
    int selection = user_Selection.nextInt();
    user_Selection.close();

    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;

            //Tests what user input was, with expected output
    if (selection  == (1)) {
        System.out.println("You chose to solve for Y: ");
        System.out.println("Y = " +slope +"("+independent_variable+")"+" + "+y_intercept);
        System.out.println("Input your answer: ");
        }
    else if (selection == (2)) {
        System.out.println("You chose to solve for M: ");
        System.out.println("M = "+"("+point_of_line_cross+" - "+y_intercept+")"+" / "+independent_variable);
        System.out.println("Input your answer: ");
        }
    else if (selection == (3)) {
        System.out.println("You chose to solve for B: ");
        System.out.println("B = "+point_of_line_cross+" - "+slope+"("+independent_variable+")");
        System.out.println("Input your answer: ");
        }
    else if (selection == (4)) {
        System.out.println("You chose to quit the program. ");
    return;
        }
        }
//Solves the problem in order to compare to User input
    int answer_y = ((m_) * (x_)) + (b_);
    int answer_m =(y_) - ((b_) / (x_));
    int answer_b =(y_) - ((m_)* (x_));
public static test_input() {
        //Problem solver defined
    Scanner answer_input = new Scanner(System.in);
    int answer = answer_input.nextInt(); 
        //Creates loop for program
    var counter = 0;
    int correct = 0;
    var answers_correct = false;
    while (!answers_correct && correct < 3) {
        if (answer == answer_y){
            counter++;
            correct++;
            System.out.println("You answered correctly");
        return;
        }
        else if (counter >= 3 && correct < 3) {
            System.out.println("Youve been missing the questions lately, let me help! ");
        }
        else
        {
            System.out.println("Try again");
            counter++;
            correct = 0;
        break;
            }
        }
    }
}

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

标签: java

解决方案


星期六我做代数已经很晚了,所以我会坚持建议改变你的程序结构。首先,您可以通过一个包含问题的类来完成所有事情,并为用户评分。该类中的方法可以通过主菜单中的菜单进行选择。我写了一个示例,说明我将如何基于标准 Java OOP 方法来构建它。在我的程序中,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

推荐阅读