【发布时间】:2015-09-30 23:04:54
【问题描述】:
当我运行我的代码时,它会一直运行,直到它询问“您想从(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 );
}
}
}
【问题讨论】:
标签: java if-statement calculator iostream