问题1:为什么long型的数据不能作为switch中表达式的值,如果写个测试程序的话会报:精度可能丢失的错误。这个你可能说是java虚拟机内部就这个规定的,如果这个的话,那我们看下面的问题。 问题2: public class JavaInt{ public static void main(String[] args){ byte b = 0; short s = 1; int i = 2; long l = 3L; int b1 = 0; int s1 = 0;
switch(b){ case 0: b1 = b + 1; break; case 1: b1 = (int)(b + 2); break; case 2: b1 = (int)(b + 3); break; case 3: b1 = (int)(b + 4); break; default: System.out.println("0===有错!"); } System.out.println(b+"");
switch(s){ case 0: s1 = s + 1; break; case 1: s1 = (int)(s + 2); break; case 2: s1 = (int)(s + 3); break; case 3: s1 = (int)(s + 4); break; default: System.out.println("1===有错!"); } System.out.println(s+"");
switch(i){ case 0: ++i; break; case 1: i = i + 2; break; case 2: i = i + 3; break; case 3: i = i + 4; break; default: System.out.println("2===有错!"); } System.out.println(i+"");
switch((int)l){ case 0: ++l; break; case 1: l = l + 2; break; case 2: l = l + 3; break; case 3: l = l + 4; break; default: System.out.println("3===有错!"); } System.out.println(l+""); } }