- import java.io.Console; //抓取鍵盤值
- public class ch25 //類別
- {
- public static void main (String arg[]) //方法
- {
- Console cs=System.console();
- int i; //輸入的數值
- int x; //亂數產生的數字
- int y=1; //計算輸入次數
- x=(int)(Math.random()*99+1);//算出1~99的亂數
- while (true)
- {
- i=Integer.parseInt(cs.readLine());
- if (x==i)
- {
- break;
- }
- else if (i<0 )
- {
- System.out.println("嘿 不在1~99範圍喔");
- y=y+1;
- }
- else if (i>99)
- {
- System.out.println("嘿 不在1~99範圍喔");
- y=y+1;
- }
- else if (x>i)
- {
- System.out.println("太小了,要再猜大一點喔");
- y=y+1;
- }
- else if (x<i)
- {
- System.out.println("太大了,要再猜小一點喔");
- y=y+1;
- continue;
- }
- }
- System.out.print("恭喜你猜對了"+"您一共猜了"+y+"次");
- }
- }
複製代碼 |