使用循环代码实现猜数字功能,代码如下:


import java.util.Scanner;
import java.util.Random;

public class GuessNumber {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        Random random = new Random();

        System.out.println("给你猜一个1-100的整数-.-!");
        int realNumber = random.nextInt(100) + 1;

        int yourGuess;
        System.out.print("请输入你猜测的整数:");
        yourGuess = reader.nextInt();

        while (yourGuess!=realNumber) {
            if (yourGuess > realNumber) {
                System.out.print("猜大了,再输入你的猜测:");
                yourGuess = reader.nextInt();
            } else if (yourGuess < realNumber) {
                System.out.print("猜小了,再输入你的猜测:");
                yourGuess = reader.nextInt();
            }
        }
        System.out.print("恭喜你,猜对了.你猜的数字是:" + realNumber);
    }
}


附上结果图片:

使用JAVA猜数字游戏


相关文章:

  • 2021-09-13
  • 2021-12-25
  • 2022-02-05
  • 2021-07-30
  • 2021-06-02
  • 2021-12-15
  • 2021-04-25
猜你喜欢
  • 2021-08-23
  • 2021-12-14
  • 2021-11-26
  • 2022-02-11
  • 2022-01-18
  • 2022-01-25
相关资源
相似解决方案