国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Java獲取一維數組的最小值實現方法

2019-11-26 15:41:29
字體:
來源:轉載
供稿:網友

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

代碼如下:

復制代碼 代碼如下:

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("獲取一維數組最小值");
        frame.setBounds(100, 100, 450, 150);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblNewLabel = new JLabel("請在文本框中輸入多個整數,以空格為分隔符。例如: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, "請輸入數字內容");
            return;
        }
        for (int i = 0; i < arrayStr.length(); i++) {                // 過濾非法輸入
            char charAt = arrayStr.charAt(i);
            if (!Character.isDigit(charAt) && charAt != ' ') {
                JOptionPane.showMessageDialog(null, "輸入包含非數字內容");
                textField.setText("");
                return;
            }
        }
        String[] numStrs = arrayStr.split(" {1,}");         // 分割字符串
        int[] numArray = new int[numStrs.length];           // 創建整型數組
        // 轉換輸入為整型數組
        for (int i = 0; i < numArray.length; i++) {
            numArray[i] = Integer.valueOf(numStrs[i]);
        }
        int min = numArray[0];                          // 創建最小數變量
        for (int j = 0; j < numArray.length; j++) {
            if (min > numArray[j]) {                 // 提取最小整數
                min = numArray[j];
            }
        }
        lblNewLabel_1.setText("數組中最小的數是:" + min);       //顯示最小值到指定的標簽中
    }
}

效果如圖所示:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 屏边| 葵青区| 盱眙县| 响水县| 游戏| 鹿邑县| 安庆市| 肥城市| 苗栗县| 朝阳区| 繁峙县| 都安| 宣武区| 丽江市| 根河市| 莒南县| 淮滨县| 阳信县| 紫阳县| 洛浦县| 十堰市| 同德县| 太和县| 小金县| 波密县| 新闻| 华亭县| 民乐县| 阿图什市| 寻乌县| 边坝县| 晴隆县| 伊川县| 辽源市| 石嘴山市| 临邑县| 阜宁县| 天台县| 山东省| 舞阳县| 宁陕县|