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次修正后,它应该循环回到程序的开头
# 1 楼答案
周六我做代数已经很晚了,所以我会坚持建议修改你的程序结构。首先,你可以通过一个包含问题的类来完成所有事情,并为用户打分。该类中的方法可以通过主菜单中的菜单进行选择。 我写了一个基于标准Java OOP方法的示例,说明了我将如何构建这个框架。在我的程序中,main不需要静态类,它循环一个菜单,并在那里选择一个问题。我的方法只有一个问题,你可以在菜单中随意添加,重要的是结构
# 2 楼答案
旁注:与要求用户输入1、2、3或4不同,您应该直接要求他们输入他们想要求解的变量:
这使得程序的用户更多地考虑问题领域,而不是一些技术上无用的间接方法
由于你有Python的背景,你应该知道行的缩进很重要,而且有意义。Java程序也是如此。唯一的区别是Java编译器完全忽略缩进。但是Java程序也可以被人类阅读,对他们来说,缩进对于理解程序的结构是可行的。你发布的代码有不一致的缩进,你应该让IDE来解决这个问题
你的程序应该是这样的:
# 3 楼答案
我想我会让你自己想办法让它循环,但我解决了你的其他问题,并把评论放在我改变的地方。希望这有帮助
`
如果你给了一个方法参数,那么当方法被调用时,你必须自己在参数中输入值。我不认为这是你想要的,因为你定义了你想要的参数值:
在test_input()方法中,您写道:
。nextInt()将使程序暂停并等待用户输入,以便在准备好获取输入之前不想使用它
我对java中的var关键字了解不多,但我认为与其使用var,不如声明变量类型,因此:
为此:
为了更好地了解这些方法的工作原理,我推荐以下两个视频: Intro to java methods Java method parameters and return types
要深入了解java的基本原理,我推荐这整张播放列表 Java Beginner Programming