代码如下:
public static void main(String[] args) { String x = ""; do { int random = (int) (Math.random()*100); Scanner scanner = new Scanner(System.in); System.out.println("请输入你要猜的数字:"); int num = scanner.nextInt(); if(num == random){ System.out.println("恭喜你,猜对了!"); }else{ System.out.println("抱歉,你猜错了!"); } System.out.println("请输入y继续?否则结束!"); x = scanner.next(); }while ("y".equals(x)); System.out.println("游戏结束!"); }
运行结果: