返回列表 發帖
  1. import java.util.Scanner;


  2. public class Hello {

  3.         public static void main(String[] args) {
  4.                 // TODO 自動產生的方法 Stub
  5.                
  6.         double x;
  7.         int y ,s=99 ,t=1 ,n=0;
  8.         System.out.println("***猜數字遊戲***");
  9.         System.out.print("請在1~99間猜一個數:");
  10.         x=(int)(Math.random()*99+1);
  11.         for(;;)
  12.         {
  13.                 try
  14.                 {
  15.                         Scanner c=new Scanner(System.in);
  16.                 n++;
  17.                 y=c.nextInt();
  18.                 if(x==y)
  19.                 {
  20.                     System.out.println("正確答案");
  21.                     System.out.println("你猜了"+n+"次");
  22.                     break;
  23.                 }else
  24.                 {
  25.                     if(y>x)
  26.                     {
  27.                         s=y-1;
  28.                     }else
  29.                     {
  30.                         t=y+1;
  31.                     }
  32.                     System.out.println("答錯了");
  33.                     System.out.print("請在"+t+"~"+s+"間猜一個數:");
  34.                 }
  35.                 }catch(Exception e)
  36.                 {
  37.                         System.out.println("你輸入錯誤!!!");
  38.                         System.out.print("請在"+t+"~"+s+"間猜一個數:");
  39.                 }
  40.         }
  41.         }

  42. }
複製代碼

TOP

返回列表