Java獲取一維數(shù)組的最小值實現(xiàn)方法
編寫程序,實現(xiàn)接受用戶在文本框中輸入的單行數(shù)據(jù)。這些數(shù)據(jù)都是整數(shù)數(shù)字,以空格進(jìn)行分隔,空格數(shù)量不限。并將這些數(shù)據(jù)分割成一維數(shù)組,再從數(shù)組中提取最小值顯示在界面中。思路是先對用戶的輸入進(jìn)行驗證,即先用trim()函數(shù)過濾用戶輸入字符串的左右空格,若結(jié)果為空字符串則用JOptionPane類的showMessageDialog方法提示用戶"請輸入數(shù)字內(nèi)容"。若用戶輸入非空則使用charAt函數(shù)對用戶輸入字符串中的每一個字符進(jìn)行判斷,若其既非數(shù)字也非空格則提示"輸入包含非數(shù)字內(nèi)容",然后使用setText()函數(shù)將用戶輸入框中的數(shù)據(jù)清空。若通過驗證則創(chuàng)建一個字符串型一維數(shù)組,其元素是用戶輸入字符串以空格分隔后得到的內(nèi)容。然后創(chuàng)建一個整型一維數(shù)組,并為其開辟等同于字符串型數(shù)組長度的空間。然后通過Integer類的valueOf()函數(shù)轉(zhuǎn)換輸入為整型數(shù)組。創(chuàng)建最小數(shù)變量,并初始化為整型數(shù)組的第一個元素。使用for循環(huán)遍歷該整型數(shù)組以提取最小整數(shù),最后使用setText()函數(shù)顯示最小值到指定的標(biāo)簽中。
代碼如下:
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("請在文本框中輸入多個整數(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, "請輸入數(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)文章
java javax.annotation.Resource注解的詳解
這篇文章主要介紹了javax.annotation.Resource注解的詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10
SpringBoot項目jar發(fā)布后如何獲取jar包所在目錄路徑
這篇文章主要介紹了SpringBoot項目jar發(fā)布后如何獲取jar包所在目錄路徑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
java 輸入3個數(shù)a,b,c,按大小順序輸出的實例講解
今天小編就為大家分享一篇java 輸入3個數(shù)a,b,c,按大小順序輸出的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
springboot整合Nacos組件環(huán)境搭建和入門案例詳解(最新推薦)
本文介紹了Nacos的基礎(chǔ)概念、關(guān)鍵特性、專業(yè)術(shù)語和生態(tài)圈,如何在Windows環(huán)境下搭建Nacos單個服務(wù),以及如何整合SpringBoot2來使用Nacos進(jìn)行服務(wù)注冊和配置管理,感興趣的朋友一起看看吧2025-03-03
springboot 整合 nacos 配置實現(xiàn)多個環(huán)境不同配置
本文介紹了Nacos配置中心的優(yōu)勢,包括與Apollo的性能對比,Nacos服務(wù)端的安裝與配置,以及如何在SpringBoot項目中集成Nacos進(jìn)行多環(huán)境配置,提供了詳細(xì)的步驟,包括下載、安裝、配置中心的創(chuàng)建和項目集成,旨在幫助開發(fā)者更好地使用Nacos進(jìn)行項目配置管理2024-09-09
java計算兩個日期之前的天數(shù)實例(排除節(jié)假日和周末)
下面小編就為大家?guī)硪黄猨ava計算兩個日期之前的天數(shù)實例(排除節(jié)假日和周末)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07

