1、switch
2、随机数Random类
案例:
随机数小游戏----猜数字
package com.itheima.random;
import java.util.Random;
import java.util.Scanner;
public class random_game {
public static void main(String[] args) {
Random random = new Random();
int r = random.nextInt(100)+1;
Scanner sc = new Scanner(System.in);
System.out.println("请猜一个数字:");
int guessNumber = sc.nextInt();
while(r != guessNumber)
{
if(guessNumber>r)
{
System.out.println("太大了,小一点!");
guessNumber = sc.nextInt();
System.out.println("您输入的是:"+guessNumber);
}
else
{
System.out.println("太小了,大一点!");
guessNumber = sc.nextInt();
System.out.println("您输入的是:"+guessNumber);
}
}
System.out.println("您猜对了");
System.out.println("随机数是"+r);
}
}
|