本帖最後由 顏宇鋒 於 2021-11-20 19:23 編輯
- import java.util.Scanner;
- public class Ch01
- {
- public static void main(String args[])
- {
- Scanner c=new Scanner(System.in);
- int a=1,b=99,d,e=0,x;
- d=((int)(Math.random()*99+1));
- for(int i=1;i<=99;i++)
- {
- if(a==b)
- break;
- System.out.print("猜一個"+a+"~"+b+"之間的數字:");
- x=c.nextInt();
- if(x>b)
- System.out.println("問號?");
- else if(x<a)
- System.out.println("問號?");
- else
- {
- if(d<x)
- {
- System.out.println("猜得太大了!");
- b=x-1;
- e++;
- }else if(d>x)
- {
- System.out.println("猜得太小了!");
- a=x+1;
- e++;
- }else
- {
- System.out.println("恭喜你猜對了!");
- System.out.println("總共猜了"+(e+1)+"次!");
- return;
- }
- }
- }
- System.out.println("可惜了答案是"+d+"!");
- }
- }
複製代碼 |