一道Java编程题
Java程序:
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Test27 extends JApplet implements KeyListener{
JTextField txt1, txt2, txt3;
public void init(){
this.setLayout(new FlowLayout());
txt1 = new JTextField(10);
txt2 = new JTextField(10);
txt3 = new JTextField(10);
txt3.addKeyListener(this);
this.add(new JLabel("输入第1个整数"));
this.add(txt1);
this.add(new JLabel("输入第2个整数"));
this.add(txt2);
this.add(new JLabel("输入第3个整数"));
this.add(txt3);
this.setSize(250, 200);
this.setVisible(true);
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == 10){//按下回车键
int num1, num2, num3;
int temp;
//获取来自文本框的值
num1 = Integer.parseInt(txt1.getText());
num2 = Integer.parseInt(txt2.getText());
num3 = Integer.parseInt(txt3.getText());
//对三个数按非递减排序
if(num1 > num2){
temp = num1;
num1 = num2;
num2 = temp;
}
if(num2 > num3){
temp = num2;
num2 = num3;
num3 = temp;
}
if(num1 > num2){
temp = num1;
num1 = num2;
num2 = temp;
}
//在状态栏显示数据
this.showStatus(num1 + ", " + num2 + ", " + num3);
//清空文本框
txt1.setText(null);
txt2.setText(null);
txt3.setText(null);
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
}
运行测试:
哈哈,你比较了两次num1和num2吧?没关系,我能看懂!
import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Arrays;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MyApplet extends Applet {
private static final long serialVersionUID = 1L;
private JLabel jl;
private JTextField jtf;
private JTextField[] text = new JTextField[3];//设置输入框数组
public MyApplet() {
jl = new JLabel("第一个整数:");
this.add(jl, BorderLayout.WEST);
jtf = new JTextField(10);
this.add(jtf);
text[0] = jtf;//输入框1放进输入框数组中
jl = new JLabel("第二个整数:");
this.add(jl, BorderLayout.WEST);
jtf = new JTextField(10);
this.add(jtf);
text[1] = jtf;//输入框2放进输入框数组中
jl = new JLabel("第三个整数:");
this.add(jl, BorderLayout.WEST);
jtf = new JTextField(10);
this.add(jtf);
//加入监听
jtf.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
int[] num = new int[3];
// 取出数据
for (int i = 0; i < num.length; i++) {
//将输入框数组转换成数组存入数组,方便排序
num[i] = Integer.parseInt(text[i].getText());
text[i].setText(null);
}
Arrays.sort(num);// 用arrays类中sort方法排序,从小到大
String str = num[0] + "--" + num[1] + "--" + num[2];
MyApplet.this.showStatus("排序大小为:" + str);
}
}
});
text[2] = jtf; //输入框3放进输入框数组中
this.setVisible(true);
}
}
我是初学者,你写的太难了……
很难么?JDK中的工具抱中有一个类是可以帮你排序的,干嘛要去自己写!!
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Test extends Applet implements ActionListener{
/**
* @param args
*/
Button button1,button2;
Panel panel,panel2;
Label label;
TextField text1,text2,text3;
int num1,num2,num3;
public void init(){
button1=new Button("确定");
button2=new Button("取消");
text1=new TextField(8);
text2=new TextField(8);
text3=new TextField(8);
panel=new Panel();
panel2=new Panel();
label=new Label();
panel.setLayout(new GridLayout(3,2,5,5));
panel2.setLayout(new GridLayout(2,2,5,5));
panel.add(new Label("请输入第1个整数"));
panel.add(text1);
panel.add(new Label("请输入第2个整数"));
panel.add(text2);
panel.add(new Label("请输入第3个整数"));
panel.add(text3);
panel2.add(button1);
panel2.add(button2);
panel2.add(label);
this.setLayout(new GridLayout(2,1));
add(panel);
add(panel2);
this.setSize(250, 200);
}
public void start(){
button1.addActionListener(this);
}
public void destroy(){
}
public void stop(){
}
public void paint(Graphics g){
g.setColor(Color.RED);
g.drawString("龙影设计", 40, 150);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Object obj = e.getSource();
if(obj==button1){
num1=Integer.parseInt(text1.getText());
num2=Integer.parseInt(text2.getText());
num3=Integer.parseInt(text3.getText());
int temp;
if(num1>num2){
temp=num1;
num1=num2;
num2=temp;
}
else{
if(num2>num3){
temp=num2;
num2=num3;
num3=temp;
}
}
String str=num1+"<"+num2+"<"+num3;
text1.setText("");
text2.setText("");
text3.setText("");
label.setText("结果为:"+str);
//System.out.println(str);
}
}
}
最后一句System.out.println(str);有误,应该是在状态栏里显示的!
public int[] mySort(int [] nums){
for(int i=0;i<nums.length;i++){
for(int j=i;j<nums.length;j++){
if(nums[i]>nums[j]){
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
}
}
}
return nums;
}
封装成一个数组放进去,无论多长都可以排序成从大到小