- import java.util.Scanner;
- public class Ch66 {
- public static void main(String[] args) {
- // TODO 自動產生的方法 Stub
- int target = (int)(Math.random()*99)+1;
- int start=1,end=99,ans,count=0;
- while(true)
- {
- try
- {
- Scanner scn = new Scanner(System.in);
- count++;
- System.out.print("請輸入一個"+start+"到"+end+"的數字:");
- ans = scn.nextInt();
- if(ans > target)
- {
- end = ans-1;
- }else if(ans < target)
- {
- start = ans+1;
- }else
- {
- System.out.println("恭喜您答對了!!!");
- System.out.println("總共猜了: "+count+"次!!");
- break;
- }
- }catch(Exception e)
- {
- System.out.print("輸入錯誤");
- }
- }
- }
- }
複製代碼 |