當我運行我的代碼時,它會正常工作,直到它提出問題「您想要使用哪種操作(sum,subst,multi,div)」。無論用戶選擇什麼,我的程序都沒有迴應!這個非常簡單的代碼有什麼問題
這是怎麼發生的?
import java.util.Scanner;
import java.io.*;
public class three3 {
public static void main (String[] args) {
int x;
int y;
int opera;
String oper;
Scanner in = new Scanner (System.in);
System.out.println(" write the first number ");
x = in.nextInt();
System.out.println(" write the second number ");
y = in.nextInt();
System.out.println(" which operation do you want to use from (sum , subst , multi , div)");
oper = in.nextLine();
if (oper == "sum") {
opera=x+y;
System.out.println(" the sum of two numbers is " + opera);
}
if (oper == "subst") {
opera = x - y;
System.out.println(" the subtraction of two numbers is " + opera);
}
if (oper == "multi") {
opera = x * y;
System.out.println(" the multi of two numbers is " + opera);
}
if (oper == "div") {
opera = x/y;
System.out.println(" the division of two numbers is " + opera);
}
}
}
這不是正確的答案 – gurghet
@gurghet我錯過了胡安的回答中的錯誤,但是我正在接受的觀點仍然是意外行爲的一個原因。 – runDOSrun
沒有*錯誤的*迴應。有*沒有*回覆 – gurghet