搜尋此網誌

2013年6月19日 星期三

猜數字範圍註解.

 import java.awt.event.ActionEvent;  
 import java.awt.event.ActionListener;  
 import javax.swing.JButton;  
 import javax.swing.JFrame;  
 import javax.swing.JLabel;  
 import javax.swing.JTextField;  
 class Frame extends JFrame implements ActionListener {  
      JTextField setnum = new JTextField();//範圍欄位  
      JTextField txtinput = new JTextField();//使用者輸入欄位  
      JTextField txtArea = new JTextField();//提示訊息欄位.  
      JLabel j = new JLabel("請輸入範圍0~?");  
      JLabel j2 = new JLabel("請輸入任意數:");  
      JButton OK = new JButton("go");//ok按鈕  
      int set, c, num;  
      int b = 0;  
      boolean setting = true;//已設定好範圍  
      boolean right = false;//答案錯誤  
      Frame() {  
           j.setBounds(20, 10, 100, 20);//設定標籤尺寸和範圍.  
           add(j);//把標籤加入frame  
           setnum.setBounds(110, 10, 80, 20);  
           setnum.addActionListener(this);//把範圍欄位加入行為監聽器  
           add(setnum);//把範圍欄位加入frame  
           j2.setBounds(20, 50, 100, 20);  
           add(j2);  
           txtinput.setBounds(110, 50, 80, 20);  
           txtinput.addActionListener(this);  
           add(txtinput);  
           txtinput.setEditable(false);  
           txtArea.setBounds(20, 80, 170, 20);  
           txtArea.setEditable(false);  
           add(txtArea);  
           OK.addActionListener(this);  
           OK.setBounds(120, 110, 70, 20);  
           add(OK);  
           setTitle("終極密碼");//設定frame名稱.  
           setLayout(null);  
           setBounds(200, 200, 220, 180);  
           setVisible(true);  
           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
      }  
      public void actionPerformed(ActionEvent e) {  
           if (e.getSource() == OK || e.getSource() == txtinput//當按下ok 或者 使用者觸發了 範圍設定欄位, 或是輸入猜測數字,則執行下列程式.  
                     || e.getSource() == setnum) {  
                if (setting) {//假如已設定範圍.  
                     set = Integer.parseInt(setnum.getText());//讀入設定的數字.  
                     c = set;//把數字設給c變數,  
                     if (set <= 1) {//假如設定的數字小於1  
                          txtArea.setText("請輸入大於1的任意數字");  
                     } else {  
                          num = (int) (Math.random() * c - 1) + 1;//亂數產生數字.  
                          setting = false;//因為已經設定了正確的範圍, 所以把範圍設定旗標, 設為false,   
                          txtArea.setText("請猜0~" + set + "的任意數字");  
                          txtinput.setEditable(true);//把欄位設為不可編輯.  
                          setnum.setEditable(false);  
                     }  
                } else {  
                     int g = Integer.parseInt(txtinput.getText());//把使用者讀入的字串轉為數字存到變數g.  
                     if (g >= c || g <= b) {//假如數字小於0或是超出設定的範圍.  
                          txtArea.setText("超出範圍" + b + "~" + c + "請從新輸入");  
                     } else {  
                          if (g == num) {  
                               txtArea.setText("恭喜答對拉!");  
                               right = true;  
                               OK.setEnabled(false);  
                          } else if (g > num) {  
                               c = g;  
                               txtArea.setText("錯瞜~小一些" + b + "~" + c);  
                               txtinput.setText("");  
                          } else if (g < num) {  
                               b = g;  
                               txtArea.setText("錯瞜~大一些" + b + "~" + c);  
                               txtinput.setText("");  
                          }  
                     }  
                }  
           }  
      }  
 }  
 public class Davinci_Code {  
      public static void main(String[] args) {  
           new Frame();  
      }  
 }  

沒有留言:

張貼留言