在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("猜小了,继续"); } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报