猜數字遊戲

import javax.swing.Icon;java

import javax.swing.JOptionPane;dom

public class CaiShuZi4JOptionPane {.net

    /**code

     * @param args遊戲

     */get

    public static void main(String[] args) {io

        Icon icon = null;class

        boolean bl = false;import

        int put = 0;隨機數

        int c = (int) (((Math.random())*100)+1);  //獲取一個1-100的隨機數

        System.out.println("你獲取的隨機數是:"+c);  //打印你的隨機數字

                                                                  

        String str1  = (String) JOptionPane.showInputDialog(null,"請輸入你的猜想數字(1-100):\n","猜數字遊戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入"); //第一次輸入你的猜想數字

                                                                      

        if(str1==null){

            JOptionPane.showMessageDialog(null"你已經取消了本次遊戲"); //若是你點取消那麼本次遊戲結束

        }else{

            bl = num(str1);  //判斷是輸入的是否是數字或者是整數

            if(true==bl){      //若是是數字的話進入與隨機數比較的程序

            System.out.println("你輸入的數字是:"+str1);  //打印你輸入的數字

             put = Integer.valueOf(str1); 

                                                                      

            for(int i = 4;i > 0;i--){    //i是你能夠猜想的次數

                 if(put==c){

                    JOptionPane.showMessageDialog(null"恭喜你猜對了,正確答案是:"+c+"。");  //若是你猜對了就直接結束循環

                    break;

                 }else if(put>c){  //若是輸大了就讓你再次重新輸入

                     str1  = (String) JOptionPane.showInputDialog(null,"你的輸入過大。你還有"+i+"次機會,請從新輸入:\n","猜數字遊戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入");

                     if(str1==null){

                         JOptionPane.showMessageDialog(null"你已經取消了本次輸入");

                         break;

                        }else{

                             bl =num(str1);

                               if(true==bl){

                                  put = Integer.valueOf(str1);

                                }else{

                                   JOptionPane.showMessageDialog(null"你的輸入不正確,請從新輸入");

                                }

                        }

                 }else if(put<c){  //若是你輸小了也讓你重新輸入

                    str1  = (String) JOptionPane.showInputDialog(null,"你的輸入太小。你還有"+i+"次機會,請從新輸入:\n","猜數字遊戲",JOptionPane.PLAIN_MESSAGE,icon,null,"在這輸入");

                     if(str1==null){

                         JOptionPane.showMessageDialog(null"你已經取消了本次輸入");

                         break;

                        }else{

                            bl =num(str1);

                           if(true==bl){

                             put = Integer.valueOf(str1);

                           }else{

                                JOptionPane.showMessageDialog(null"你的輸入不正確,請從新輸入");

                            }

                      }

                }

            }

                                                                      

                                                                          

        }else if(bl==false){   //這個 是你第一次若是填寫的不是數字的話也會結束本次遊戲

            JOptionPane.showMessageDialog(null"請您下次按要求填寫。本次遊戲結束");

        }

        if(true==bl && c!=put){  //若是你i次都沒猜對,那麼就直接告訴你這個數十什麼

                 JOptionPane.showMessageDialog(null"很遺憾你沒能猜對,這個數字是:"+c+".");

         }

                                                                          

      }

                                                                       

    }

                                                                  

    public  static boolean num(String value){  //一個靜態方法,判斷你輸入的是否是數字

       try {

        Integer.parseInt(value);

        return true;

        catch (Exception e) {

        return false;

        }

                                                                     

   }

}

相關文章
相關標籤/搜索