public static void main(String[] args) {
int day = 1;
switch(day) {
case 1:
System.out.println("星期一");
break;
case 2:
System.out.println("星期二");
break;
case 3:
System.out.println("星期三");
break;
case 4:
System.out.println("星期四");
break;
case 5:
System.out.println("星期五");
break;
case 6:
System.out.println("星期六");
break;
case 7:
System.out.println("星期日");
break;
default:
System.out.println("输入有误");
break;
}
}
public static void main(String[] args) {
System.out.println("hello world");
System.out.print("hello world ");
int x = 10;
System.out.printf("x = %d\n", x);
}
public static void main5(String[] args) {
Scanner scanner = new Scanner(System.in);
int Age = scanner.nextInt();
while(Age>0) {
if ( Age <= 18) {
System.out.println("少年");
break;
} else if (Age > 18 && Age <= 28) {
System.out.println("青年");
break;
} else if (Age > 28 && Age <= 55) {
System.out.println("中年");
break;
} else {
System.out.println("老年");
break;
}
}
}
打印结果:
2. 判定一个数字是否是素数
代码实现:
public static void main4(String[] args) {
Scanner scanner = new Scanner(System.in);
int Num = scanner.nextInt();
int i;
for (i = 2; i < Num; i++) {
if (Num % i == 0) {
System.out.println(Num + "不是素数");
break;
}
}
if (Num==i){
System.out.println(Num+"是素数");
}
}
打印结果:
3.输出乘法口诀表
代码实现:
public static void main(String[] args) {
for (int i = 1; i <= 9 ; i++) {
for (int j = 1; j <= i ; j++) {
System.out.print(i+"*"+j+"="+i*j+" ");
}
System.out.println();
}
}
public static void login(){
Scanner scanner = new Scanner(System.in);
int count = 3;
while(count != 0){
System.out.println("请输入你的密码:");
String password = scanner.nextLine();
if(password.equals("123456")){
System.out.println("登陆成功了!");
break;
}else{
count--;
System.out.println("你输错密码了,你还有 "+count+"次机会!");
}
}
}
public static void main16(String[] args) {
login();
}
打印结果:
这里用到了方法,后面在说。
5.猜数字游戏
代码实现:
public static void main(String[] args) {
Random random = new Random();
int rand = random.nextInt(100);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("请输入你要猜的数字:");
int n = scanner.nextInt();
if (n < rand) {
System.out.println("低了");
} else if (n > rand) {
System.out.println("高了");
} else {
System.out.println("猜对了");
break;
}
}
}