package com.karl; import java.util.Random; import java.util.Scanner; public class GameDemo { public static void main(String[] args) { //1.生成一个随机号码 Random r=new Random(); int l = r.nextInt(100)+1; //定义一个死循环 Scanner sc=new Scanner(System.in); while (true){ //提供用户猜测 System.out.println("输入猜测号码:"); int da=sc.nextInt(); //判断 if (da>l){ System.out.println("过大"); }else if(da<l){ System.out.println("过小"); }else { System.out.println("猜对了"); break;//结束死循环 } } } }
标签:Scanner,int,Random,System,da,Game,死循环 From: https://www.cnblogs.com/Karl-hut/p/17437994.html