最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java獲取一維數(shù)組的最小值實(shí)現(xiàn)方法

 更新時(shí)間:2014年02月18日 17:33:25   作者:  
這篇文章主要介紹了Java獲取一維數(shù)組的最小值實(shí)現(xiàn)方法,需要的朋友可以參考下

編寫程序,實(shí)現(xiàn)接受用戶在文本框中輸入的單行數(shù)據(jù)。這些數(shù)據(jù)都是整數(shù)數(shù)字,以空格進(jìn)行分隔,空格數(shù)量不限。并將這些數(shù)據(jù)分割成一維數(shù)組,再從數(shù)組中提取最小值顯示在界面中。思路是先對(duì)用戶的輸入進(jìn)行驗(yàn)證,即先用trim()函數(shù)過濾用戶輸入字符串的左右空格,若結(jié)果為空字符串則用JOptionPane類的showMessageDialog方法提示用戶"請(qǐng)輸入數(shù)字內(nèi)容"。若用戶輸入非空則使用charAt函數(shù)對(duì)用戶輸入字符串中的每一個(gè)字符進(jìn)行判斷,若其既非數(shù)字也非空格則提示"輸入包含非數(shù)字內(nèi)容",然后使用setText()函數(shù)將用戶輸入框中的數(shù)據(jù)清空。若通過驗(yàn)證則創(chuàng)建一個(gè)字符串型一維數(shù)組,其元素是用戶輸入字符串以空格分隔后得到的內(nèi)容。然后創(chuàng)建一個(gè)整型一維數(shù)組,并為其開辟等同于字符串型數(shù)組長度的空間。然后通過Integer類的valueOf()函數(shù)轉(zhuǎn)換輸入為整型數(shù)組。創(chuàng)建最小數(shù)變量,并初始化為整型數(shù)組的第一個(gè)元素。使用for循環(huán)遍歷該整型數(shù)組以提取最小整數(shù),最后使用setText()函數(shù)顯示最小值到指定的標(biāo)簽中。

代碼如下:

復(fù)制代碼 代碼如下:

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;

public class ArrayMinValue {

    private JFrame frame;
    private JTextField textField;
    JLabel lblNewLabel_1 = new JLabel();
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ArrayMinValue window = new ArrayMinValue();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public ArrayMinValue() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame("獲取一維數(shù)組最小值");
        frame.setBounds(100, 100, 450, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblNewLabel = new JLabel("請(qǐng)?jiān)谖谋究蛑休斎攵鄠€(gè)整數(shù),以空格為分隔符。例如:3 5 2 562 125");
        lblNewLabel.setBounds(10, 10, 414, 15);
        frame.getContentPane().add(lblNewLabel);

        textField = new JTextField();
        textField.setBounds(10, 35, 414, 21);
        frame.getContentPane().add(textField);
        textField.setColumns(10);      
        lblNewLabel_1.setBounds(115, 70, 309, 15);
        frame.getContentPane().add(lblNewLabel_1);
        JButton button = new JButton("\u8BA1\u7B97");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_actionPerformed(e);
            }
        });
        button.setBounds(10, 66, 93, 23);
        frame.getContentPane().add(button);    
    }
    protected void do_button_actionPerformed(ActionEvent e) {
        String arrayStr = textField.getText().trim();           //去除左右空格
        if(arrayStr.equals("")){
            JOptionPane.showMessageDialog(null, "請(qǐng)輸入數(shù)字內(nèi)容");
            return;
        }
        for (int i = 0; i < arrayStr.length(); i++) {                // 過濾非法輸入
            char charAt = arrayStr.charAt(i);
            if (!Character.isDigit(charAt) && charAt != ' ') {
                JOptionPane.showMessageDialog(null, "輸入包含非數(shù)字內(nèi)容");
                textField.setText("");
                return;
            }
        }
        String[] numStrs = arrayStr.split(" {1,}");         // 分割字符串
        int[] numArray = new int[numStrs.length];           // 創(chuàng)建整型數(shù)組
        // 轉(zhuǎn)換輸入為整型數(shù)組
        for (int i = 0; i < numArray.length; i++) {
            numArray[i] = Integer.valueOf(numStrs[i]);
        }
        int min = numArray[0];                          // 創(chuàng)建最小數(shù)變量
        for (int j = 0; j < numArray.length; j++) {
            if (min > numArray[j]) {                 // 提取最小整數(shù)
                min = numArray[j];
            }
        }
        lblNewLabel_1.setText("數(shù)組中最小的數(shù)是:" + min);       //顯示最小值到指定的標(biāo)簽中
    }
}

效果如圖所示:

相關(guān)文章

最新評(píng)論

习水县| 长白| 盐边县| 嵩明县| 池州市| 西城区| 扶风县| 宜宾市| 鸡东县| 渝中区| 福州市| 荔浦县| 乌拉特前旗| 宿州市| 普定县| 青龙| 吉林市| 云梦县| 福贡县| 威海市| 波密县| 高雄县| 资溪县| 扎囊县| 庆阳市| 锡林郭勒盟| 珠海市| 哈密市| 新竹市| 视频| 沧州市| 景宁| 会宁县| 哈巴河县| 田林县| 扶沟县| 祁东县| 涡阳县| 新闻| 阿克| 察隅县|