生成随机数,用户猜数字

编写一个猜数字的游戏,由计算机随机产生一个数字(1-100),由用户去猜,只显示大了或小了,直到猜中为止

源代码

//编写一个猜数字的游戏,由计算机随机产生一个数字(1-100),由用户去猜,只显示大了或小了,直到猜中为止
import java.util.Random;
import java.util.Scanner;

public class sjs {
    public static void main(String[] args) {
        Random x=new Random();
        int y=x.nextInt(100)+1;//生成随机数
        Scanner sc=new Scanner(System.in);
        System.out.println("请您输入数字");
        while(true){
            int a=sc.nextInt();
            if(a==y)
            {
                System.out.println("恭喜您,您猜中了");
                break;
            }else{
                if(a<y){
                    System.out.println("您输入的数字小了");
                }
                else{
                    System.out.println("您输入的数字大了");
                }
            }
        }
        System.out.println("随机数是"+y);
    }
}

运行截图