编写一个猜数字的游戏1-100(程序运行一次,猜错继续,猜中结束-递归/循环)

1-100猜数字

public class Cz {
    
    
    //编写一个猜数字的游戏1-100
    public static void main(String[] args) {
    
    
        //设置随机数
        int a = (int) (Math.random() * 100+1);
        int g;
        System.out.println("欢迎猜数字!");
        do {
    
    
            System.out.println("请输入一个1-100的数字");
            Scanner sc = new Scanner(System.in);
            g = sc.nextInt();
            if (g > a){
    
    
                System.out.println("大了");
            }else if (g < a){
    
    
                System.out.println("小了");
            }
        }while (g!=a);
        System.out.println("中了");
    }
}

猜你喜欢

转载自blog.csdn.net/BO2345/article/details/125778619