JAVA比较2个数大小

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA比较2个数大小相关的知识,希望对你有一定的参考价值。

package zuoye1;
import java.awt.*;
import java.awt.event.*;

public class ActionEventDemol implements WindowListener
Frame f = new Frame("ActionEventDemol");
Label msg = new Label("第一个数:",Label.CENTER);
Label msg1 = new Label("第二个数:",Label.CENTER);
Label msg2 = new Label("大的数:",Label.CENTER);
Label msg3 = new Label("",Label.CENTER);

TextField name = new TextField(10);

TextField output = new TextField(10);
Button bConfirm = new Button("确定");
Button bReset = new Button("重置");
Listener lsn = new Listener(this);
public ActionEventDemol()
f.setLayout(null);
f.add(msg);
f.add(msg2);
f.add(name);
f.add(output);
f.add(bConfirm);
f.add(bReset);
f.add(msg1);
msg.setBounds(20,40,120,10);
msg1.setBounds(20,60,120,10);
msg2.setBounds(20,90,120,10);
msg3.setBounds(50,90,120,10);
name.setBounds(140, 40, 140, 20);
output.setBounds(140, 60,140, 20);
bConfirm.setBounds(40, 110, 70, 20);
bReset.setBounds(190, 110, 70, 20);
f.setSize(300,150);
f.setVisible(true);
bConfirm.addActionListener(lsn);
bReset.addActionListener(lsn);
f.addWindowListener(this);


public static void main(String[] args)
new ActionEventDemol();


public void windowActivated(WindowEvent e)
public void windowDeactivated(WindowEvent e)
public void windowIconified(WindowEvent e)
public void windowDeiconified(WindowEvent e)
public void windowOpened(WindowEvent e)
public void windowClosed(WindowEvent e)
public void windowClosing(WindowEvent e)
System.exit(0);

class Listener implements ActionListener

ActionEventDemol ob;
Listener(ActionEventDemol ob)
this.ob = ob;

public void actionPerformed(ActionEvent e)

if(e.getSource() == ob.bConfirm)
String s1 = name.getText();
String s2 = output.getText();

double d1;
double d2;
try
d1 =Double.parseDouble(s1);
d2 =Double.parseDouble(s2);
if(d1>d2)
(msg3.setText(s1);)
else(msg3.setText(s2);)
catch (NumberFormatException e1)
label4.setText("请输入数字");

else if(e.getSource()==ob.bReset)
ob.name.setText("");
ob.output.setText("");






问题在那里?

你的代码我改了一下
这个应该是符合你的要求的
import java.awt.*;
import java.awt.event.*;

public class ActionEventDemol implements WindowListener
Frame f = new Frame("ActionEventDemol");
Label msg = new Label("第一个数:",Label.CENTER);
Label msg1 = new Label("第二个数:",Label.CENTER);
Label msg2 = new Label("大的数:",Label.CENTER);
Label msg3 = new Label("",Label.CENTER);

TextField name = new TextField(10);

TextField output = new TextField(10);
Button bConfirm = new Button("确定");
Button bReset = new Button("重置");
Listener lsn = new Listener(this);
public ActionEventDemol()
f.setLayout(null);
f.add(msg);
f.add(msg2);
f.add(name);
f.add(output);
f.add(bConfirm);
f.add(bReset);
f.add(msg1);
f.add(msg3);
msg.setBounds(20,40,120,10);
msg1.setBounds(20,60,120,10);
msg2.setBounds(20,90,50,10);
msg3.setBounds(30,90,120,10);
name.setBounds(140, 40, 140, 20);
output.setBounds(140, 60,140, 20);
bConfirm.setBounds(40, 110, 70, 20);
bReset.setBounds(190, 110, 70, 20);
f.setSize(300,150);
f.setVisible(true);
bConfirm.addActionListener(lsn);
bReset.addActionListener(lsn);
f.addWindowListener(this);


public static void main(String[] args)
new ActionEventDemol();


public void windowActivated(WindowEvent e)
public void windowDeactivated(WindowEvent e)
public void windowIconified(WindowEvent e)
public void windowDeiconified(WindowEvent e)
public void windowOpened(WindowEvent e)
public void windowClosed(WindowEvent e)
public void windowClosing(WindowEvent e)
System.exit(0);

class Listener implements ActionListener

ActionEventDemol ob;
Listener(ActionEventDemol ob)
this.ob = ob;

public void actionPerformed(ActionEvent e)
if(e.getSource() == ob.bConfirm)
String s1 = name.getText();
String s2 = output.getText();

double d1;
double d2;
try
d1 =Double.parseDouble(s1);
d2 =Double.parseDouble(s2);

if(d1>d2)
msg3.setText(s1);
else
msg3.setText(s2);
name.setText("");
output.setText("");
catch (NumberFormatException e1)
msg3.setText("请输入数字");

else if(e.getSource()==ob.bReset)
ob.name.setText("");

ob.output.setText("");


参考技术A package com.baidu;

import java.awt.*;
import java.awt.event.*;

public class ActionEventDemol implements WindowListener
Frame f = new Frame("ActionEventDemol");
Label msg = new Label("第一个数:", Label.CENTER);
Label msg1 = new Label("第二个数:", Label.CENTER);
Label msg2 = new Label("大的数:", Label.CENTER);
Label msg3 = new Label("信息", Label.CENTER);
TextField name = new TextField(10);

TextField output = new TextField(10);
Button bConfirm = new Button("确定");
Button bReset = new Button("重置");
Listener lsn = new Listener(this);

public ActionEventDemol()
f.setLayout(null);
f.add(msg);
f.add(msg1);
f.add(msg2);
f.add(msg3);
f.add(name);
f.add(output);
f.add(bConfirm);
f.add(bReset);
msg.setBounds(20, 40, 120, 10);
msg1.setBounds(20, 60, 120, 10);
msg2.setBounds(20, 90, 120, 10);
msg3.setBounds(140, 90, 120, 10);
name.setBounds(140, 40, 140, 20);
output.setBounds(140, 60, 140, 20);
bConfirm.setBounds(40, 110, 70, 20);
bReset.setBounds(190, 110, 70, 20);
f.setSize(300, 150);
f.setVisible(true);
bConfirm.addActionListener(lsn);
bReset.addActionListener(lsn);
f.addWindowListener(this);



public static void main(String[] args)
new ActionEventDemol();


public void windowActivated(WindowEvent e)


public void windowDeactivated(WindowEvent e)


public void windowIconified(WindowEvent e)


public void windowDeiconified(WindowEvent e)


public void windowOpened(WindowEvent e)


public void windowClosed(WindowEvent e)


public void windowClosing(WindowEvent e)
System.exit(0);


class Listener implements ActionListener

ActionEventDemol ob;

Listener(ActionEventDemol ob)
this.ob = ob;


public void actionPerformed(ActionEvent e)
if (e.getSource() == ob.bConfirm)
String s1 = name.getText();
String s2 = output.getText();
System.out.println(s1+s2);
double d1;
double d2;
try
d1 = Double.parseDouble(s1);
d2 = Double.parseDouble(s2);
if (d1 > d2)
msg3.setText(s1);
else
msg3.setText(s2);

catch (NumberFormatException e1)
msg3.setText("请输入数字");

else if (e.getSource() == ob.bReset)
ob.name.setText("");
ob.output.setText("");






代码很乱,改了一下,应该是你要的结果
中间的错误主要有几处:
if(d1>d2)
(msg3.setText(s1);)
else(msg3.setText(s2);)
catch (NumberFormatException e1)
label4.setText("请输入数字");


if(条件)后面应该接一对else后面同理
label4.setText("请输入数字");应该是msg3.setText()
你label4根本就是未定义的
还有 你msg3这个标签没有加入panel中,并且,你设置的坐标也不对
括号不成对这个问题很严重。
参考技术B 好乱的代码啊,看了头疼
package zuoye1;
import java.awt.*;
import java.awt.event.*;

public class ActionEventDemol implements WindowListener
Frame f = new Frame("ActionEventDemol");
Label msg = new Label("第一个数:",Label.CENTER);
Label msg1 = new Label("第二个数:",Label.CENTER);
Label msg2 = new Label("大的数:",Label.CENTER);
Label msg3 = new Label("",Label.CENTER);

TextField name = new TextField(10);

TextField output = new TextField(10);
Button bConfirm = new Button("确定");
Button bReset = new Button("重置");
Listener lsn = new Listener(this);
public ActionEventDemol()
f.setLayout(null);
f.add(msg);
f.add(msg2);
f.add(msg3); //你忘记把msg3加进去了
f.add(name);
f.add(output);
f.add(bConfirm);
f.add(bReset);
f.add(msg1);
msg.setBounds(20,40,120,20);
msg1.setBounds(20,60,120,20);
msg2.setBounds(20,90,120,20);
msg3.setBounds(140,90,140,20);
name.setBounds(140, 40, 140, 20);
output.setBounds(140, 60,140, 20);
bConfirm.setBounds(40, 110, 70, 20);
bReset.setBounds(190, 110, 70, 20);
f.setSize(300,150);
f.setVisible(true);
bConfirm.addActionListener(lsn);
bReset.addActionListener(lsn);
f.addWindowListener(this);

public static void main(String[] args)
new ActionEventDemol();


public void windowActivated(WindowEvent e)
public void windowDeactivated(WindowEvent e)
public void windowIconified(WindowEvent e)
public void windowDeiconified(WindowEvent e)
public void windowOpened(WindowEvent e)
public void windowClosed(WindowEvent e)
public void windowClosing(WindowEvent e)
System.exit(0);

class Listener implements ActionListener

ActionEventDemol ob;
Listener(ActionEventDemol ob)
this.ob = ob;

public void actionPerformed(ActionEvent e)
if(e.getSource() == ob.bConfirm)
String s1 = ob.name.getText();
String s2 = ob.output.getText();

double d1;
double d2;
try
d1 =Double.parseDouble(s1);
d2 =Double.parseDouble(s2);
if(d1>d2)
msg3.setText(s1);
elsemsg3.setText(s2);
catch (NumberFormatException e1)
msg3.setText("请输入数字");


else if(e.getSource()==ob.bReset)
ob.name.setText("");
ob.output.setText("");



参考技术C import java.awt.*;
import java.awt.event.*;

public class ActionEventDemol implements WindowListener

Frame f = new Frame("ActionEventDemol");
Label msg = new Label("第一个数:",Label.CENTER);
Label msg1 = new Label("第二个数:",Label.CENTER);
Label msg2 = new Label("大的数:",Label.CENTER);
Label msg3 = new Label("",Label.CENTER);

TextField name = new TextField(10);

TextField output = new TextField(10);
Button bConfirm = new Button("确定");
Button bReset = new Button("重置");
Listener lsn = new Listener(this);

public ActionEventDemol()
f.setLayout(null);
f.add(msg);
f.add(msg2);
f.add(name);
f.add(output);
f.add(bConfirm);
f.add(bReset);
f.add(msg1);
f.add(msg3);
msg.setBounds(20,40,120,10);
msg1.setBounds(20,60,120,10);
msg2.setBounds(20,90,120,10);
msg3.setBounds(60,90,200,10);
name.setBounds(140, 40, 140, 20);
output.setBounds(140, 60,140, 20);
bConfirm.setBounds(40, 110, 70, 20);
bReset.setBounds(190, 110, 70, 20);
f.setSize(300,150);
f.setVisible(true);
bConfirm.addActionListener(lsn);
bReset.addActionListener(lsn);
f.addWindowListener(this);


public static void main(String[] args)
new ActionEventDemol();


public void windowActivated(WindowEvent e)
public void windowDeactivated(WindowEvent e)
public void windowIconified(WindowEvent e)
public void windowDeiconified(WindowEvent e)
public void windowOpened(WindowEvent e)
public void windowClosed(WindowEvent e)
public void windowClosing(WindowEvent e)
System.exit(0);




class Listener implements ActionListener

ActionEventDemol ob;

Listener(ActionEventDemol ob)
this.ob = ob;


private String maxdouble(double d1,double d2)
if(d1>d2) return String.valueOf(d1);
else return String.valueOf(d2);


private String max(String s1,String s2)
if(s1 != null && s2 != null)
try
return maxdouble(Double.parseDouble(s1),Double.parseDouble(s2));
catch (NumberFormatException e1)
ob.name.setText("请输入数字!!!");


return null;


public void actionPerformed(ActionEvent e)
if(e.getSource() == ob.bConfirm)
ob.msg3.setText(max(ob.name.getText(),ob.output.getText()));

else if(e.getSource()==ob.bReset)
ob.name.setText("");
ob.output.setText("");


java中不同类型数比大小的程序代码

可以使用java.math.BigDecimal类型,把不同类型的数统一成这个类型,然后用compareTo方法比较,1、0、-1分别表示大于、等于、小于,例如:比较1.22 和 5,类声明和main方法声明略,直接写内容
BigDecimal num1 = new BigDecimal(1.22);
BigDecimal num2 = new BigDecimal(5);
if(num1.compareTo(num2) > 0)
// 1.22比5大
else if (num1.compareTo(num2) < 0)
// 1.22比5小
else
// 1.22与5相等
参考技术A 肯定得转换成为相同类型才能比较大小阿 参考技术B int 、float等基本数据类型用==
string 用eques

以上是关于JAVA比较2个数大小的主要内容,如果未能解决你的问题,请参考以下文章

关于java 窗口大小的设定问题。

C语言中,怎么比较三个数的大小??

java中不同类型数比大小的程序代码

用c#比较三个数的大小,并输出最大数。

比较2个数的大小

如何查看java一个类的大小