可能重复:
变量未初始化 - 虽然我是?
我对编程很陌生,所以我想尝试制作一个简单的计算器,它并不特定于像 + 这样的符号。无论如何,我不太确定为什么这不起作用,因为据我所知,我做的一切都是正确的。
import java.util.Scanner;
public class calculator2 {
public static void main(String args[]){
Scanner tom = new Scanner(System.in);
double fnum, snum, answer;
int symbol;
System.out.println("Enter First Number");
fnum = tom.nextDouble();
System.out.println("Enter Comand, 1 = add, 2 = subtract, 3 = divide, 4 = multiply");
symbol = (int)tom.nextDouble();
System.out.println("Enter Second Number");
snum = tom.nextDouble();
switch (symbol){
case 1:
answer = fnum + snum;
case 2:
answer = fnum - snum;
case 3:
answer = fnum / snum;
case 4:
answer = fnum * snum;
}
System.out.println(answer);
}
}