编写程序,实现接受用户在文本框中输入的单行数据。这些数据都是整数数字,以空格进行分隔,空格数量不限。并将这些数据分割成一维数组,再从数组中提取最小值显示在界面中。思路是先对用户的输入进行验证,即先用trim()函数过滤用户输入字符串的左右空格,若结果为空字符串则用JOptionPane类的showMessageDialog方法提示用户"请输入数字内容"。若用户输入非空则使用charAt函数对用户输入字符串中的每一个字符进行判断,若其既非数字也非空格则提示"输入包含非数字内容",然后使用setText()函数将用户输入框中的数据清空。若通过验证则创建一个字符串型一维数组,其元素是用户输入字符串以空格分隔后得到的内容。然后创建一个整型一维数组,并为其开辟等同于字符串型数组长度的空间。然后通过Integer类的valueOf()函数转换输入为整型数组。创建最小数变量,并初始化为整型数组的第一个元素。使用for循环遍历该整型数组以提取最小整数,最后使用setText()函数显示最小值到指定的标签中。
代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 |
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); //显示最小值到指定的标签中 } } |
效果如图所示:
原文:http://www.cnblogs.com/cysolo/p/3552830.html