这是整个项目,它应该尽我所能,但我希望在这里发布它可以让我看到我错过的东西,我有适度的java经验
package com.company;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
Scanner input = new Scanner(System.in);
int number1;
int number2;
int answer;
String operator;
这表明用户想要的数字和运营商
System.out.println("Please Enter your first number");
number1 = input.nextInt();
System.out.println("Please enter your second number");
number2 = input.nextInt();
System.out.println("Please enter your operator: + , - , * , / ");
operator = input.next();
if语句应该确定用户想要哪个运算符并将其应用于这两个数字,然后调用randomEquation方法使其成为错误的答案
if (operator == "+") {
answer = number1 + number2;
System.out.println("Your answer is: " + randomEquation(answer));
} else if (operator == "-") {
answer = number1 - number2;
System.out.println("Your answer is: " + randomEquation(answer));
} else if (operator == "*") {
answer = number1 * number2;
System.out.println("Your answer is: " + randomEquation(answer));
} else if (operator == "/") {
answer = number1 / number2;
System.out.println("Your answer is: " + randomEquation(answer));
}
}
此方法随机将其中一个应用于答案以创建错误的答案
public static int randomEquation(int number) {
Random rand = new Random();
int random = rand.nextInt(100) + 1;
int answer = number;
if (random <= 100 && random >= 81) {
answer = number * 25;
return answer;
}
else if(random <= 80 && random >= 61){
answer += 13;
return answer;
}
else if(random <= 60 && random >= 41){
answer /= 2;
return answer;
}
else if(random <= 40 && random >= 21){
answer -= 16;
return answer;
}
else{
answer %= 4;
return answer;
}
}
}
答案 0 :(得分:1)
你需要在字符串中使用.equals。
而不是:
operator == "+"
尝试:
"+".equals(operator)