好的,所以我正在制作一个简单的用户输入类型的计算器,提供加、减、乘、除选项。当我运行程序时,一切都很顺利,直到我按下回车键,希望得到我的结果.......没有任何反应。并且 eclipse 给出的唯一警告是未使用局部变量 answer 的值....这是我的代码:
class apples {
public static void main(String args[]) {
System.out
.println("To Add Press 1 , To Subtract Press 2, To Multiply Press 3, To Divide Press 4");
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
double fnum, snum, answer;
if (i == 1) {
System.out.println("Enter Your First Number: ");
fnum = sc.nextDouble();
System.out.println("Enter Your Second Number");
snum = sc.nextDouble();
answer = fnum + snum;
} else if (i == 2) {
System.out.println("Enter Your First Number: ");
fnum = sc.nextDouble();
System.out.println("Enter Your Second Number");
snum = sc.nextDouble();
answer = fnum - snum;
} else if (i == 3) {
System.out.println("Enter Your First Number: ");
fnum = sc.nextDouble();
System.out.println("Enter Your Second Number");
snum = sc.nextDouble();
answer = fnum * snum;
} else if (i == 4) {
System.out.println("Enter Your First Number: ");
fnum = sc.nextDouble();
System.out.println("Enter Your Second Number");
snum = sc.nextDouble();
answer = fnum / snum;
}
}
}