赞
踩
- import java.util.Random;
- import java.util.Scanner;
-
- public class GuessNum {
- public static void main(String[] args) {
- Random random = new Random();
- int Num = random.nextInt(100) + 1;
- int count = 0;
- boolean guessed = false;
-
- Scanner scanner = new Scanner(System.in);
- System.out.println("欢迎来到猜数字游戏!");
-
- while (!guessed) {
- System.out.print("请输入一个数字(1-100):");
- int guess = scanner.nextInt();
- count++;
-
- if (guess == Num) {
- System.out.println("恭喜你,猜对了!你猜了" + count + "次。");
- guessed = true;
- } else if (guess < Num) {
- System.out.println("猜小了,请继续猜。");
- } else {
- System.out.println("猜大了,请继续猜。");
- }
- }
-
- scanner.close();
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。