返回列表 發帖

猜數字遊戲

本帖最後由 tonyh 於 2012-9-22 16:23 編輯

設計一猜數字遊戲, 讓使用者猜一個1~99間的數字,
正確答案暫定為38, 若猜大了則顯示"猜得太大了", 反之則顯示"猜得太小了",
運用continue與break語法, 讓使用者能反覆猜測, 直到猜對了才跳出程式.

  1. import java.io.Console;
  2. public class ch28
  3. {
  4.      public static void main(String args[])
  5.      {
  6.        Console console=System.console();
  7.        String str;
  8.        int ans=38;
  9.        int guess;
  10.        while (true)
  11.        {
  12.          System.out.print("猜一個1~99的數字");
  13.          guess=Integer.parseInt(console.readLine());
  14.          if(guess!=ans)
  15.          {
  16.            str=(guess>ans)?"猜的太大了":"猜的太小了";
  17.            System.out.println(str);
  18.            continue;
  19.          }
  20.          break;
  21.        }
  22.        System.out.println("恭喜你猜對了");
  23.      }
  24. }
複製代碼

TOP

  1. public class ch28
  2. {
  3.      public static void main(String args[])
  4.      {
  5.        Console console=System.console();
  6.        String str;
  7.        int ans=38;
  8.        int guess;
  9.        while (true)
  10.        {
  11.          System.out.print("猜一個1~99的數字");
  12.          guess=Integer.parseInt(console.readLine());
  13.          if(guess!=ans)
  14.          {
  15.            str=(guess>ans)?"猜的太大了":"猜的太小了";
  16.            System.out.println(str);
  17.            continue;
  18.          }
  19.          break;
  20.        }
  21.        System.out.println("恭喜你猜對了");
  22.      }
  23. }
複製代碼

TOP

  1. import java.io.Console;
  2. public class ch28
  3. {
  4.     public static void main(String args[])
  5.     {
  6.         Console console=System.console();
  7.         String str;
  8.         int ans=38;
  9.         int guess;
  10.         while(true)
  11.         {
  12.             System.out.print("猜一個1~99間的數字: ");
  13.             guess=Integer.parseInt(console.readLine());
  14.             if(guess!=ans)
  15.             {
  16.                 str=(guess!=ans)?"猜的太大了":"猜的太小了";
  17.                 System.out.println(str);
  18.                 continue;
  19.             }
  20.             break;
  21.         }
  22.         System.out.println("恭喜你猜對了!");
  23.     }
  24. }
複製代碼

TOP

  1. public class ch28
  2. {
  3.      public static void main(String args[])
  4.      {
  5.        Console console=System.console();
  6.        String str;
  7.        int ans=38;
  8.        int guess;
  9.        while (true)
  10.        {
  11.          System.out.print("猜一個1~99的數字");
  12.          guess=Integer.parseInt(console.readLine());
  13.          if(guess!=ans)
  14.          {
  15.            str=(guess>ans)?"猜的太大了":"猜的太小了";
  16.            System.out.println(str);
  17.            continue;
  18.          }
  19.          break;
  20.        }
  21.        System.out.println("恭喜你猜對了");
  22.      }
  23. }
複製代碼

TOP

  1. import java.io.Console;
  2. public class ch28
  3. {
  4.    public static void main(String args[])
  5.    {
  6.          Console console=System.console();
  7.          String str;
  8.          int ans=38;
  9.          int guess;
  10.          while(true)
  11.          {
  12.           System.out.print("猜一個1~99間的數字");
  13.           guess=Integer.parseInt(console.readLine());
  14.           if(guess!=ans)
  15.           {
  16.             str=(guess>ans)?"猜得太大了":"猜得太小了";
  17.             System.out.println(str);
  18.             continue;
  19.           }
  20.           break;
  21.          }
  22.          System.out.println("恭喜你猜對了");
  23.    }
  24. }
複製代碼

TOP

  1. import java.io.Console;
  2. public class ch28
  3. {
  4.      public static void main(String args[])
  5.      {
  6.        Console console=System.console();
  7.        String str;
  8.        int ans=38;
  9.        int guess;
  10.        while (true)
  11.        {
  12.          System.out.print("猜一個1~99的數字");
  13.          guess=Integer.parseInt(console.readLine());
  14.          if(guess!=ans)
  15.          {
  16.          str=(guess>ans)?"猜的太大了":"猜的太小了";
  17.          System.out.println(str);
  18.          continue;
  19.          }
  20.          break;
  21.        }
  22.        System.out.println("恭喜你猜對了");
  23.      }
  24. }
複製代碼

TOP

  1. import java.io.Console;
  2. public class ch28
  3. {
  4.    public static void main(String args[])
  5.    {
  6.      Console console=System.console();
  7.      String str;
  8.      int ans=38;
  9.      int guess;
  10.      while(true)
  11.      {
  12.         System.out.print("猜一個1~99的數字: ");
  13.         guess=Integer.parseInt(console.readLine());
  14.         if(guess!=ans)
  15.         {
  16.            str=(guess>ans)?"猜得太大了!":"猜得太小了!";
  17.            System.out.println(str);
  18.            continue;
  19.         }
  20.      System.out.println("賓果!");
  21.            break;
  22.      }
  23.    }
  24. }
複製代碼

TOP

返回列表