①:
package Z1;
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入会员号");
int a = sc.nextInt();
System.out.println("请输入会员生日:(格式为月/日,例如:01/05)");
String b = sc.next();
System.out.println("请输入会员积分:");
double c = sc.nextDouble();
if (b.length() != 4 && b.indexOf('.') != -1) {
// indexOf:返回指定字符在此字符串中第一次出现处的索引。
System.out.println("会员生日输入错误");
} else if (a < 1000 || a > 9999) {
System.out.println("会员号输入错误");
} else if (c < 100) {
System.out.println("会员积分过低,请及时充值");
} else {
System.out.println(a + "\\t" + b + "\\t" + c);
}
}
}
②: 5岁一下的孩子不能搬桌子。5到7岁的男孩可以搬,女孩不可以搬。7岁以上都可以搬。
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入年龄:");
int i = input.nextInt();
System.out.println("请输入性别:");
String j = input.next();
switch (i) {
case 1:
case 2:
case 3:
case 4:
System.out.println("搬不动。");
break;
case 5:
if (j.charAt(0) == '男') {
System.out.println("可以搬动桌子。");
} else {
System.out.println("搬不动。");
}
break;
case 6:
if (j.charAt(0) == '男') {
System.out.println("可以搬动桌子。");
} else {
System.out.println("搬不动。");
}
break;
default:
System.out.println("可以搬动桌子。");
}
}
}
③:迷你计算器
import java.util.Scanner;
public class Test7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数");
double a = sc.nextDouble();
System.out.println("请输入第二个数");
double b = sc.nextDouble();
System.out.println("请输入运算符");
String c = sc.next();
double d = 0;
switch (c.charAt(0)) {
case '+':
d = a + b;
System.out.println(d);
break;
case '-':
d = a - b;
System.out.println(d);
break;
case '*':
d = a * b;
System.out.println(d);
break;
case '/':
d = a / b;
System.out.println(d);
break;
default:
System.out.println("输入运算符错误");
break;
}
}
}