Java 猜数游戏
題目描述
以下程序隨機(jī)產(chǎn)生一個1~100的整數(shù),用戶通過鍵盤輸入所猜的數(shù),如果猜對,則結(jié)束程序;如果猜錯,則給出提示繼續(xù)猜,直到猜對為止。Math.random()可以隨機(jī)產(chǎn)生一個[0,1)之間的實數(shù)。請將程序填寫完整。
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("恭喜,這個數(shù)為"+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("恭喜,這個數(shù)為" + t + "!");break;}}} }總結(jié)
- 上一篇: Java x和y之间的全部素数
- 下一篇: java美元兑换,(Java实现) 美元