维拉斯斯 2021-11-11 00:29 采纳率: 53.3%
浏览 15
已结题

在java中怎么用if else 与while 制作简单的猜数游戏

在java中怎么用if else 与while 制作简单的猜数游戏

  • 写回答

1条回答 默认 最新

  • -Undefined_ 2021-11-11 00:39
    关注
    public static void main(String[] args){
            Scanner sc = new Scanner(System.in);
            Random r = new Random();
            int real = r.nextInt(100);
            int guess;
            while(true){
                System.out.println("请猜数字(0~100的整数):");
                guess = sc.nextInt();
                if(guess == real){
                    System.out.println("猜对了!");
                    break;
                }
                if(guess>real){
                    System.out.println("猜大了,继续");
                }else {
                    System.out.println("猜小了,继续");
                }
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 12月21日
  • 已采纳回答 12月13日
  • 创建了问题 11月11日