返回列表 發帖

猜數字遊戲

本帖最後由 tonyh 於 2012-10-10 19:36 編輯

設計一猜數字遊戲, 猜一介於1~99間的數字,
此數字由電腦隨機亂數產生,
使用者可重覆猜測, 且範圍會越縮越小,
最後猜中後, 計算使用者總共猜了幾次才猜中.
  1. import java.io.Console;
  2. public class ch25{
  3.     public static void main(String args[])
  4.     {
  5.           Console console=System.console();
  6.           String str;
  7.           int a=1, b=99, count=0;
  8.           int ans=(int)(Math.random()*99+1);
  9.           int guess;
  10.           while(true)
  11.           {
  12.                count++;
  13.                System.out.print("猜一個"+a+"~"+b+"之間的數字: ");
  14.                guess=Integer.parseInt(console.readLine());
  15.                a=(guess<ans)?guess:a;
  16.                b=(guess>ans)?guess:b;
  17.                if(guess!=ans)
  18.                {
  19.                     str=(guess>ans)?"猜得太大了":"猜得太小了";
  20.                     System.out.println(str);
  21.                     continue;
  22.                }
  23.                break;
  24.           }
  25.           System.out.println("恭喜你猜對了!");
  26.           System.out.println("總共猜了"+count+"次!");
  27.     }
  28. }
複製代碼
  1. package tony.java.practice;
  2. import java.util.Scanner;
  3. public class ch25{
  4.     public static void main(String args[])
  5.     {
  6.           Scanner scanner=new Scanner(System.in);
  7.           String str;
  8.           int a=1, b=99, count=0;
  9.           int ans=(int)(Math.random()*99+1);
  10.           int guess;
  11.           while(true)
  12.           {
  13.                count++;
  14.                System.out.print("猜一個"+a+"~"+b+"之間的數字: ");
  15.                guess=scanner.nextInt();
  16.                a=(guess<ans)?guess:a;
  17.                b=(guess>ans)?guess:b;
  18.                if(guess!=ans)
  19.                {
  20.                     str=(guess>ans)?"猜得太大了":"猜得太小了";
  21.                     System.out.println(str);
  22.                     continue;
  23.                }
  24.                break;
  25.           }
  26.           System.out.println("恭喜你猜對了!");
  27.           System.out.println("總共猜了"+count+"次!");
  28.     }
  29. }
複製代碼

本帖最後由 孫家祥 於 2012-10-10 20:24 編輯
  1. package sun.java.test;

  2. import java.io.Console;
  3. import java.util.Scanner;

  4. public class gsnum {


  5.     public static void main(String args[])
  6.     {
  7.             Scanner scanIn=new Scanner(System.in);
  8.           int v,w,x=5000,y,z,int1,int2;
  9.           String str="請開始猜整數~",str2;
  10.           y=1;
  11.           v=((int)(Math.random() * 3000)+30+y);
  12.           w=((int)(Math.random() * 6)+y);
  13.           v=(v-(v%w));
  14.           int1=(v-(int)(Math.random() * 30));
  15.           int2=(v+(int)(Math.random() * 30));
  16.           System.out.println("請輸入介於"+int1+"至"+int2+"中,"+w+"倍數之整數");
  17.           z=0;
  18.           while (x!=v && z<9)
  19.           {
  20.           System.out.println(str);
  21.           x=Integer.parseInt(scanIn.next());
  22.           str=(x>v)? "過大了,請再猜~介於"+int1+"至"+(x-1)+"中,"+w+"倍數之整數":"過小了,請再猜~介於"+(x+1)+"至"+int2+"中,"+w+"倍數之整數";
  23.           z+=1;
  24.           }
  25.           str2=(((z==9)? "很抱歉!已猜錯"+z+"次!!":"恭喜您猜對了,沒錯!")+"正確數值是: "+v);
  26.           System.out.println(str2);
  27.           System.out.println("你總共猜了"+z+"遍");
  28.     }

  29. }
複製代碼

TOP

  1. import java.io.Console;
  2. public class ch116
  3. {
  4.   public static void main (String args[])
  5.   {   
  6.      int x, big, small, i, a;
  7.      big=99;
  8.      Small=1;
  9.      i=1;
  10.      System.out.print("數字遊戲, 猜一介於1~99間的數字");
  11.       System.out.println();
  12.        Console console=System.console();
  13.       System.out.print("請猜一個0~99的數: ");
  14.       a=Integer.parseInt(console.readLine());


  15.       x=((int)(Math.random()*100+1));  //產生介於1~99的隨機亂數;

  16.         while(a==x)(
  17.       if(a>x)
  18.         {
  19.            big==a ;
  20.            System.out.println("猜一介於"+big"~"+small"間的數字");
  21.            Console console=System.console();
  22.       a=Integer.parseInt(console.readLine());
  23.            i++;
  24.         }else if(a<x)
  25.         {
  26.            small==a;
  27.            System.out.println("猜一介於"+big"~"+small"間的數字");
  28.            Console console=System.console();
  29.       a=Integer.parseInt(console.readLine());
  30.            i++;

  31.         }
  32.          else
  33.         {

  34.         }

  35.         )
  36.           System.out.println(+a);
  37.             System.out.println("恭喜你猜對了,你共猜了"+i"次");


  38. }
  39.   }
複製代碼

TOP

  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

  1. import java.io.Console; //引入java.io類別
  2. public class ch25 //continue & break
  3. {
  4. public static void main(String args[])
  5. {
  6.   int guest,anwser,cycle;
  7.   int start_num, end_num;
  8.   Console console = System.console();
  9.   
  10.   anwser = (int)(Math.random()*100+1);
  11.   //System.out.println(anwser);
  12.   
  13.   System.out.println();
  14.   System.out.println("這是一個猜數字的遊戲,請在1-99之間,猜一數,");
  15.   System.out.println("準備好了嗎...Let's Go!!");
  16.   System.out.println();
  17.   
  18.   cycle = 1;
  19.   start_num = 1;
  20.   end_num = 99;
  21.   
  22.   while(true)
  23.   {
  24.     System.out.print("請輸入數字("+start_num+"-"+end_num+"):");
  25.     guest = Integer.parseInt(console.readLine());
  26.        
  27.         if(guest==anwser) break;
  28.        
  29.         if(guest > anwser)
  30.         {
  31.                 System.out.println("****猜錯囉,請猜小一點****");
  32.                 System.out.println();
  33.                 cycle++;
  34.                 end_num = guest;
  35.                 continue;
  36.         }
  37.        
  38.         if(guest < anwser)
  39.         {
  40.                 System.out.println("****猜錯囉,請猜大一點****");
  41.                 System.out.println();
  42.                 cycle++;
  43.                 start_num = guest;
  44.                 continue;
  45.         }
  46.   
  47.   }
  48.   
  49.   System.out.println("****恭喜你猜中了,"+"一共猜了"+cycle+"次!!****");
  50. }
  51. }
複製代碼

TOP

本帖最後由 吳尚哲 於 2012-10-9 11:42 編輯
  1. import java.io.Console;

  2. public class ch23
  3. {
  4.   public static void main (String args[])
  5.   {
  6.         Console console=System.console();
  7.         int a, upper=99, lower=1, q, count=0;
  8.         q = ((int)(Math.random()*99)+1);                                                //產生1~99間之亂數
  9.         while(true)
  10.         {
  11.                 System.out.print("請猜一個介於" + lower + "~" + upper +"的數字: ");
  12.                 a=(int)(Float.parseFloat(console.readLine()));                //防呆用
  13.                 while( a > upper || a < lower)                                                //防呆用
  14.                 {
  15.                         System.out.println("請'正確'猜一個介於" + lower + "~" + upper +"的數字");
  16.                         a=(int)(Float.parseFloat(console.readLine()));        //防呆用
  17.                 }
  18.                 count++;
  19.                 if(a > q)
  20.                 {
  21.                         System.out.println(a + "太大囉~");
  22.                         upper = a-1;
  23.                 }
  24.                 else if(a < q)
  25.                 {
  26.                         System.out.println(a + "太小囉~");
  27.                         lower = a+1;
  28.                 }
  29.                 else
  30.                 {
  31.                         System.out.println();
  32.                         System.out.println("恭喜您答對囉^^");
  33.                         System.out.println("您一共猜了" + count + "次!!");
  34.                         break;
  35.                 }       
  36.         }
  37.   }
  38. }
複製代碼

TOP

本帖最後由 晏有聰 於 2012-10-9 09:36 編輯
  1. /* 猜數字遊戲1-100,會計算猜幾次 */
  2. import java.io.Console;
  3. public class ch26_2
  4. {
  5.    public static void main (String showdemo[])
  6.    {
  7.       Console console=System.console();
  8.    
  9.       int random_value,input1,count=1;
  10.       int range1=1,range2=100;      
  11.       random_value=(int)((Math.random())*100+1);
  12.       
  13.       System.out.print("請猜測一個數字介於1-100的整數=");  
  14.       input1=Integer.parseInt(console.readLine());
  15.       
  16.       while (true)
  17.       {
  18.          if(input1>0 && input1<=100)
  19.          {            
  20.                if(input1==random_value)
  21.                {
  22.                   System.out.print("Bingo,您猜對了  ");
  23.                   System.out.println("您猜了"+count+"次");
  24.                   break;                 
  25.                }         
  26.                else
  27.                {
  28.                    range2=((input1>random_value)&&(input1<range2))?input1:range2;                                
  29.                              
  30.                    range1=((input1<random_value)&&(input1>range1))?input1:range1;                  
  31.                  
  32.                    System.out.print("請再猜一次,範圍介於 "+range1+"~"+range2+"=");

  33.                    input1=Integer.parseInt(console.readLine());

  34.                    count=count+1;

  35.                    continue;  
  36.                }                              
  37.          }
  38.          else
  39.          {
  40.              System.out.print("來亂的嗎 ? 請猜1-100之間的整數=");
  41.              count=count+1;
  42.          }
  43.          input1=Integer.parseInt(console.readLine());     
  44.       }

  45.    }
  46. }
複製代碼

TOP

返回列表