//输入一个值x后,判断x的范围后进行运算
public static void test1() {
int y, t = 0;
System.out.println("请输入x的值");
Scanner sc = new Scanner(System.in);
short x = sc.nextShort();
if (x > 0) {
t = 1;
} else if (x < 0) {
t = 2;
} else if (x == 0) {
t = 0;
}
switch (t) {
case 0:
y = x + 20;
System.out.println("y的值为" + y);
break;
case 1:
y = x + 10 * 5;
System.out.println("y的值为" + y);
break;
case 2:
y = x + 50 / 3;
System.out.println("y的值为" + y);
break;
}
本文介绍了如何使用Java编写一个简单的程序,根据用户输入的short类型数值x,判断其范围并执行相应的运算,输出y的值。
64

被折叠的 条评论
为什么被折叠?



