‘壹’ java中两个字符串如何比较大小
有三种方法实现
第一种直接用字符串类的compareTo方法:
Stringt1="20131011";
Stringt2="20131030";
intresult=t1.compareTo(t2);
第二种是把这个日期字符串转换成long:
SimpleDateFormatsdf=newSimpleDateFormat("yyyyMMdd");
Dated1=sdf.parse(t1);
Dated2=sdf.parse(t2);
longresult=d1.getTime()-d2.getTime();
第三种是把日期字符串转换成整形int:
intint1=Integer.parseInt(t1);
intint2=Integer.parseInt(t2);
intresult=int1-int2;
注:result大于0,则t1>t2;
result等于0,则t1=t2;
result小于0,则t1<t2;
‘贰’ java中double与Integer直接比较大小是否可以
当然可以直接比较了,比较中会把integer的转型为double再比较大小的
‘叁’ Java中Double的比较
Double没有的,double有 Double是对象,看两个对象是否值相同,调用 equals方法
这也是解决double精度比较的方法。另外还有一种方法,你可以用写一个 double成员属性,然后在eclipse中重写equals方法,你会看到他是怎么处理两个double值是否相同的 sun公司用的是Double.doubleToLongBits(要比较的double值) != Double.doubleToLongBits(另一个double值) 这种方式
‘肆’ JAVA比较2个数大小
你的代码我改了一下
这个应该是符合你的要求的
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("");
}
}
}
‘伍’ java中double类型怎么判断大于0
double i =8;
if(i>0){
System.out.println("大于0");
}
‘陆’ java double和float的哪个大
float:4个字节,32位
double:8个字节,64位
‘柒’ java小数比较
double类型的数值比较应该使用差值的绝对值
例如:public class DoubleCompare
{
public static void main(String[] args)
{
float a = 0.6f;
double b = 0.6;
System.out.println(a == b);
System.out.println(Math.abs(a - b) <= 0.0000001);
}
}
第一个打印为false,第二个为true