编写一个字符界面的Java Application程序。接受用户输入的10个整数,并输出这10个整数的最大值

 我来答
大臧358
2014-07-09 · 超过60用户采纳过TA的回答
知道答主
回答量:115
采纳率:100%
帮助的人:57.7万
展开全部
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); } }
匿名用户
2013-09-09
展开全部
给你一个方法 是随意输入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();
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2013-09-09
展开全部
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条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式