有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

  • 如果3次正确,则结束程序并输出总正确的尝试次数
  • 否则,如果有3次尝试,则输出为提示

我面临的主要问题是这两种方法的循环。如果我的代码糟糕透顶,我提前道歉,与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次修正后,它应该循环回到程序的开头


共 (3) 个答案

  1. # 1 楼答案

    周六我做代数已经很晚了,所以我会坚持建议修改你的程序结构。首先,你可以通过一个包含问题的类来完成所有事情,并为用户打分。该类中的方法可以通过主菜单中的菜单进行选择。 我写了一个基于标准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
    
  2. # 2 楼答案

    旁注:与要求用户输入1、2、3或4不同,您应该直接要求他们输入他们想要求解的变量:

    Solve the equation y = m * x + b for which variable (y, m, b, quit)?

    这使得程序的用户更多地考虑问题领域,而不是一些技术上无用的间接方法


    由于你有Python的背景,你应该知道行的缩进很重要,而且有意义。Java程序也是如此。唯一的区别是Java编译器完全忽略缩进。但是Java程序也可以被人类阅读,对他们来说,缩进对于理解程序的结构是可行的。你发布的代码有不一致的缩进,你应该让IDE来解决这个问题


    你的程序应该是这样的:

    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();
        }
    }
    
  3. # 3 楼答案

    我想我会让你自己想办法让它循环,但我解决了你的其他问题,并把评论放在我改变的地方。希望这有帮助

    //declared variables here. global variables must be declared static when accessed in a static method (ex: user_input())
    static int y_;
    static int m_;
    static int b_;
    static int x_;
    
    public static void main(String[] args) {
        // Creates scanner for input of menu Def as menu selector
        Scanner user_Selection = new Scanner(System.in);
    
        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");
    
        //Converts user input to an integer
        int selection = user_Selection.nextInt();
    
        //call user_input()
        user_input(selection);
    
    
    }
    
    public static void user_input(int selection) {
    
        Scanner user_Selection = new Scanner(System.in);
        int userAnswer;
        int y_intercept = (int) (Math.floor(Math.random() * 201) - 100);
        int slope = (int) Math.floor(Math.random() * 201) - 100;
        int point_of_line_cross = (int) Math.floor(Math.random() * 201) - 100;
        int independent_variable = (int) Math.floor(Math.random() * 201) - 100;
    
        y_ = point_of_line_cross;
        m_ = slope;
        b_ = y_intercept;
        x_ = independent_variable;
    
    
        //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: ");
            userAnswer = user_Selection.nextInt();
            /*After user enters answer we test the answer by calling test_input
             * */
           test_input(userAnswer);
        } 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: ");
            userAnswer = user_Selection.nextInt();
            /*After user enters answer we test the answer by calling test_input
             * */
            test_input(userAnswer);
        } 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: ");
            userAnswer = user_Selection.nextInt();
            /*After user enters answer we test the answer by calling test_input
             * */
            test_input(userAnswer);
        } else if (selection == (4)) {
            System.out.println("You chose to quit the program. ");
        }
    }
    
    // you forgot to include return type ex: void, int, String, double, float, etc
    public static void test_input(int entered_answer) {
        //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_));
    
        //Problem solver defined
        int answer = entered_answer;
    
        //Creates loop for program
        int counter = 0;
        int correct = 0;
        boolean 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("You've been missing the questions lately, let me help! ");
            } else {
                System.out.println("Try again");
                counter++;
                correct = 0;
                break;
            }
        }
    
    }
    

    `

    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;
    

    在test_input()方法中,您写道:

    Scanner answer_input = new Scanner(System.in);
    int answer = answer_input.nextInt();
    

    。nextInt()将使程序暂停并等待用户输入,以便在准备好获取输入之前不想使用它

    我对java中的var关键字了解不多,但我认为与其使用var,不如声明变量类型,因此:

    var counter = 0;
    

    为此:

    int counter = 0;
    

    为了更好地了解这些方法的工作原理,我推荐以下两个视频: Intro to java methods Java method parameters and return types

    要深入了解java的基本原理,我推荐这整张播放列表 Java Beginner Programming