返回列表 發帖
  1. package mhjhmtc;

  2. import java.util.Scanner;

  3. public class Mhjhmtc {
  4.         public static void main(String[] args)  {
  5.                 // TODO 自動產生的方法 Stub
  6.                 int answer = (int) (Math.random() * 99 + 1);
  7.                 int low = 1;
  8.                 int high = 100;
  9.                 System.out.println("請輸入" + low + "~" + high + "的數");
  10.                 int times = 0;
  11.                 int guess=0;
  12.                 while (true) {
  13.                         try {
  14.                                 Scanner scanner = new Scanner(System.in);
  15.                         guess = scanner.nextInt();
  16.                         }
  17.                         catch (Exception ex) {
  18.                   guess=0;
  19.                         }
  20.                         if (guess < answer) {
  21.                                 low = guess;
  22.                                 System.out.println("數字太小了喔");
  23.                                 System.out.println("請輸入" + low + "~" + high + "的數");
  24.                                 times++;
  25.                         }
  26.                         else if (guess > answer) {
  27.                                 high = guess;
  28.                                 System.out.println("數字太大了喔");
  29.                                 System.out.println("請輸入" + low + "~" + high + "的數");
  30.                                 times++;
  31.                         }
  32.                        
  33.                         else {
  34.                                 System.out.println("答對了");
  35.                                 System.out.println("總共達對了" + times);
  36.                         }
  37.                 }
  38.         }
  39. }
複製代碼

TOP

返回列表