Java 猜数游戏

题目描述

以下程序随机产生一个1~100的整数,用户通过键盘输入所猜的数,如果猜对,则结束程序;如果猜错,则给出提示继续猜,直到猜对为止。Math.random()可以随机产生一个[0,1)之间的实数。请将程序填写完整。

import java.util.Scanner;

public class Main {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		int t =1+ 1;2(true) {
    
    
			int k = sc.nextInt();
			if (k > t)
				System.out.println("太大");3】
				System.out.println("太小");
			else {
    
    
				System.out.println("恭喜,这个数为"+t+"!");4;
			}
		}

	}
}

程序代码

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int t= (int)(Math.random() * 100 + 1);
        while (true) {
    
    
            int k = sc.nextInt();
            if (k > t)
                System.out.println("太大");
            else if(k < t)
                System.out.println("太小");
            else {
    
    
                System.out.println("恭喜,这个数为" + t + "!");
                break;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_44989881/article/details/112642052