返回列表 發帖
  1. import java.util.Scanner;
  2. public class Haha
  3. {
  4.         public static void main(String[] args)
  5. {
  6.     int start=1,end=99,count=0;
  7.     int ans=(int)(Math.random()*99+1);
  8.     int guess;
  9.     while(true)
  10.     {
  11.         try
  12.     {
  13.         Scanner scn = new Scanner(System.in);       
  14.             count++;
  15.             System.out.println("猜一個"+start+"到"+end+"之間的數字:");
  16.             guess=c.nextInt();
  17.             if(guess > ans)
  18.             {
  19.                     end = guess-1;
  20.             }else if(guess < ans)
  21.             {
  22.                     start = guess+1;
  23.             }else
  24.             {
  25.                     System.out.println("恭喜你猜對了!!!");
  26.                     System.out.println("總共猜了: "+count+"次~~~");
  27.                     break;
  28.             }
  29.     }
  30.         catch(Exception e)
  31.         {
  32.                 System.out.println("輸入錯誤!!!請輸入整數!");
  33.         }
  34.         }
  35. }
  36. }
複製代碼

TOP

  1. import java.util.Scanner;
  2. public class Haha
  3. {
  4.         public static void main(String[] args)
  5. {
  6.     int start=1,end=99,count=0;
  7.     int ans=(int)(Math.random()*99+1);
  8.     int guess;
  9.     while(true)
  10.     {
  11.         try
  12.     {
  13.         Scanner scn = new Scanner(System.in);       
  14.             count++;
  15.             System.out.println("猜一個"+start+"到"+end+"之間的數字:");
  16.             guess=scn.nextInt();
  17.             if(guess > ans)
  18.             {
  19.                     System.out.println("猜太大了!!");
  20.                     end = guess;
  21.             }else if(guess < ans)
  22.             {
  23.                     System.out.println("猜太小了!!");
  24.                     start = guess;
  25.             }else
  26.             {
  27.                     System.out.println("恭喜你猜對了!!!");
  28.                     System.out.println("總共猜了: "+count+"次~~~");
  29.                     break;
  30.             }
  31.     }
  32.         catch(Exception e)
  33.         {
  34.                 System.out.println("輸入錯誤!!!請輸入整數!");
  35.         }
  36.         }
  37. }
  38. }
複製代碼

TOP

返回列表