日期:2014-05-20 浏览次数:20611 次
import java.util.*; public class RandomNum { public static void main(String[] args) { Random random = new Random(); Scanner sc = new Scanner(System.in); int guess = 0; int guessNum = 0; String again = null; do { guess = random.nextInt(100); do { try { System.out.println("请输入您猜到的数字:"); guessNum = sc.nextInt(); if (guessNum == guess) { System.out.println("恭喜!猜对了!"); break; } else if (guessNum > guess) { System.out.println("大了!"); } else { System.out.println("小了!"); } } catch (InputMismatchException e) { System.out.println("请输入正确格式的数字!"); } } while (true); System.out.println("again?(y/n)"); again = sc.next(); if (again.equals("n")) { System.out.println("End of program."); break; } } while (true); } }