返回列表 發帖
  1. import java.io.Console; //抓取鍵盤值
  2. public class ch25 //類別
  3. {
  4.   public static void main (String arg[]) //方法
  5.   {
  6.    Console cs=System.console();  
  7.    int i;        //輸入的數值
  8.    int x;        //亂數產生的數字
  9.    int y=1;      //計算輸入次數
  10.    x=(int)(Math.random()*99+1);//算出1~99的亂數
  11.    while (true)
  12.    {
  13.     i=Integer.parseInt(cs.readLine());
  14.     if (x==i)
  15.         {
  16.           break;       
  17.         }
  18.     else if (i<0 )
  19.     {       
  20.         System.out.println("嘿 不在1~99範圍喔");
  21.         y=y+1;
  22.         }
  23.         else if (i>99)
  24.     {       
  25.         System.out.println("嘿 不在1~99範圍喔");
  26.         y=y+1;
  27.         }
  28.     else if (x>i)   
  29.         {
  30.           System.out.println("太小了,要再猜大一點喔");
  31.           y=y+1;             
  32.     }  
  33.         else if (x<i)  
  34.         {                  
  35.           System.out.println("太大了,要再猜小一點喔");
  36.           y=y+1;         
  37.       continue;
  38.         }       
  39.         }
  40.          System.out.print("恭喜你猜對了"+"您一共猜了"+y+"次");               
  41.         }
  42. }
複製代碼

TOP

返回列表