- import java.util.Scanner;
- public class bbb{
- public static void main(String args[]) {
-
- int a=(int)(Math.random()*99+1);
- int x=1,y=99,t=0;
- while(true)
- {
- try{
- Scanner c=new Scanner(System.in);
- System.out.print("請猜一個"+x+"~"+y+"之間的整數: ");
- t++;
- int b=c.nextInt();
- if(b<x||b>y)
- System.out.println("超出範圍了");
- else
- {
- if(b<a)
- x=b;
- if(b>a)
- y=b;
- if(b==a)
- break;
- }
- }catch(Exception e){
- System.out.println("請輸入整數");
- }
- }
- if(t==100)
- System.out.println("猜"+t+"次了 You Lose 正確答案="+a);
- else
- System.out.println("猜中了 正確答案="+a+"\n共猜了"+t+"次");
- }
- }
複製代碼 |