gpt4 book ai didi

java - 在连续正确回答问题 3 次/并添加方差后,我如何将程序循环回到开头

转载 作者:太空宇宙 更新时间:2023-11-04 09:43:41 25 4
gpt4 key购买 nike

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

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

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

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

public static void user_input(int point_of_line_cross, int slip, int y_intercept, int Independent_variable) {}

公共(public)静态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 个正确之后,它应该循环回到程序的开头。

最佳答案

好吧,我想我会让你弄清楚如何让它自己循环,但我解决了你的其他问题,并在我改变的地方发表了评论。希望这有帮助

//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

关于java - 在连续正确回答问题 3 次/并添加方差后,我如何将程序循环回到开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55670924/

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