编写一个字符界面的Java Application程序。接受用户输入的10个整数,并输出这10个整数的最大值
- 提问者网友:浩歌待明月
- 2021-07-25 19:17
- 五星知识达人网友:轮獄道
- 2021-07-25 19:54
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class test {
public static void main(String[] args) {
JFrame frame = new JFrame("Find Max Value");
frame.setSize(300, 300);
frame.setLocation(100, 100);
final JTextField f = new JTextField();
f.setColumns(20);
FlowLayout fl = new FlowLayout(FlowLayout.CENTER, 100, 0);
frame.setLayout(fl);
frame.add(f);
JButton b = new JButton("找出最大值");
final JLabel l = new JLabel("null");
frame.add(l);
frame.add(b);
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String s = f.getText();
if (s.length() == 0 || s.matches("[^\\d\\.]+") ) {
l.setText("输入有误!");
return;
}
String[] line = s.split("[\\s]+");
double max = Double.parseDouble(line[0]);
for(int i = 1; i < line.length; ++i) {
double a = Double.parseDouble(line[i]);
if (a > max) max = a;
}
l.setText("最大值:" + max);
}
});
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//-----------add by 小月------
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test extends JFrame implements ActionListener{
private Container c;
private TextField[] text;
private JButton btn;
private JLabel result;
public Test(){
setBounds(100, 200, 800, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c = getContentPane();
text = new TextField[10];
JPanel NORTH = new JPanel(new FlowLayout());
c.add(NORTH,BorderLayout.NORTH);
JPanel center = new JPanel(new FlowLayout());
result = new JLabel("");
center.add(result);
c.add(center);
for(int i=0;i<text.length;i++){
text[i] = new TextField(4);
NORTH.add(text[i]);
}
btn = new JButton("计算");
c.add(btn,BorderLayout.SOUTH);
btn.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
List list = new ArrayList();
for(int i=0;i<text.length;i++){
String str = text[i].getText();
if(str==null||"".equals(str)){
result.setText("输入10个整数");
return;
}
list.add(Integer.parseInt(str));
}
Collections.sort(list);
result.setText("最大的数值是:"+list.get(9));
}
public static void main(String[] args)
{
new Test().setVisible(true);
}
}
- 1楼网友:舊物识亽
- 2021-07-25 20:34
给你一个方法 是随意输入N个数字 输出最大的数字
public static int max(String str){ String[] st = str.split(" "); int tep=0; for(int i=1;i<st.length;i++) { if(tep<Integer.parseInt(st[i].toString())) { tep= Integer.parseInt(st[i].toString()); } } return tep; } public static void main(String args[]){ String str=""; while(true) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); try { System.out.println("请输入数字且用空格隔开:"); str = in.readLine(); System.out.println("最大的数是:"); System.out.println(max(str)); } catch (IOException e) { e.printStackTrace(); } }