返回列表 發帖
  1. import java.util.Scanner;
  2. public class bbb{
  3.         public static void main(String args[]) {
  4.                
  5.                 int a=(int)(Math.random()*99+1);
  6.                 int x=1,y=99,t=0;
  7.                 while(true)
  8.                 {
  9.                         try{
  10.                                 Scanner c=new Scanner(System.in);
  11.                                 System.out.print("請猜一個"+x+"~"+y+"之間的整數: ");
  12.                                 t++;
  13.                                 int b=c.nextInt();
  14.                                 if(b<x||b>y)
  15.                                         System.out.println("超出範圍了");
  16.                                 else
  17.                                 {
  18.                                         if(b<a)
  19.                                                 x=b;
  20.                                         if(b>a)
  21.                                                 y=b;
  22.                                         if(b==a)
  23.                                                 break;
  24.                                 }
  25.                         }catch(Exception e){
  26.                                 System.out.println("請輸入整數");
  27.                         }
  28.                 }
  29.                 if(t==100)
  30.                         System.out.println("猜"+t+"次了   You Lose   正確答案="+a);
  31.                 else
  32.                         System.out.println("猜中了 正確答案="+a+"\n共猜了"+t+"次");
  33.         }

  34. }
複製代碼

TOP

返回列表