2 回答
TA贡献1856条经验 获得超17个赞
您需要将值存储在变量中,例如:
int x = ((int)(Math.random() * 200) -99));
此外,您的 for 循环是不必要的,因为它们只运行一次,因此您可以只运行程序而不是循环一次。
所以最后你的代码应该是这样的:
import java.util.Scanner;
class AlgebraTutor {
private static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int x = ((int)(Math.random() * 200) -99);
int m = ((int)(Math.random() * 200) -99);
int b = ((int)(Math.random() * 200) -99);
System.out.println("X = " + x);
System.out.println("M = " + m);
System.out.println("B = " + b);
System.out.println("Y = " + m*x+b);
}
}
要让它检查答案是对还是错,您必须执行以下操作:
import java.util.Scanner;
class AlgebraTutor {
private static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
int x = ((int)(Math.random() * 200) -99);
int m = ((int)(Math.random() * 200) -99);
int b = ((int)(Math.random() * 200) -99);
int y = m*x+b;
System.out.println("X = " + x);
System.out.println("M = " + m);
System.out.println("B = " + b);
int INPUT = in.nextInt();
if(INPUT == y){
System.out.println("You Got It Right!");
System.out.println("Y = " + y);
} else {
System.out.println("You Got It Wrong :(");
System.out.println("The Answer was: Y = " + y);
}
}
}
TA贡献1829条经验 获得超13个赞
您示例中的变量 x、m 和 b 在每个 for 循环的范围内,但}在循环之后死亡。
int x = (int)(Math.random() * 200) -99); // these variables will live long enough.
int m = (int)(Math.random() * 200) -99);
int b = (int)(Math.random() * 200) -99);
System.out.println("X = " + x);
System.out.println("M = " + m);
System.out.println("B = " + b);
System.out.print("Y = " + m * x + b);
这应该有效,但请注意,当您尝试将这些数字除以“x/b”时要小心 - 您需要强制转换为浮点数(谷歌除以整数)
添加回答
举报