java中实数比较问题

yeruping 2008-10-03 12:24:20

public class MathTest {

public void test11(String[] s){
BigDecimal[] d = new BigDecimal[s.length];
for(int i=0,ii=s.length;i<ii;i++){
d[i] = new BigDecimal(s[i]);//s[i]先转换成double类型也不行
}
for(int i=0,ii=d.length;i<ii;i++){
for(int j=i+1,len=s.length;i<len;i++){
if(d[i].compareTo(d[j])<0){
BigDecimal temp = d[i];
d[i] = d[j];
d[j] = temp;
}
}
}
for(int i=0,ii=d.length;i<ii;i++){
System.out.println(d[i]);
}
}




public static void main(String[] args) {
String[] s="23414","1.3453","431241.3451531","432143.3412","432143.3413","432143.3411"};
new MathTest().test11(s);

}



实数比较用BigDecimal怎么结果顺序还是不变?
要怎么样比较?
...全文
324 23 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
huoyin 2008-10-03
  • 打赏
  • 举报
回复
也可以用Arrays.sort()来进行比较

import java.math.*;
import java.util.*;

public class MathTest {

public static void main(String[] args) {
String[] s={"23414","1.3453","431241.3451531","432143.3412","432143.3413","432143.3411"};
Arrays.sort(s, new Comparator(){
public int compare(Object o1, Object o2){
return new BigDecimal(o1.toString()).compareTo(new BigDecimal(o2.toString()));
}
});
for(int i=0; i<s.length;i++){
System.out.println(s[i]);
}
}
}
justinavril 2008-10-03
  • 打赏
  • 举报
回复
确实是for循环没写好...
yeruping 2008-10-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 accp206 的回复:]
呵呵,还是3楼更快。

其实那句代码可以写得更简单一些:
for(int j=i+1;j <s.length;j++)
其它循环中也没有必要单独再定义一个变量去保存数组的长度

这是我改完之后的代码:

Java code
import java.math.BigDecimal;

public class MathTest {

public void test11(String[] s){
BigDecimal[] d = new BigDecimal[s.length];
for(int i=0;i<s.length;i++){
d[i] = new…
[/Quote]
N个实数的话 我觉得有必要,像示例那么少的话,可以不用
N个效率会好点
yeruping 2008-10-03
  • 打赏
  • 举报
回复
谢谢 你们了 ....看样子得早点休息了 头脑不行了 ..
accp206 2008-10-03
  • 打赏
  • 举报
回复
呵呵,还是3楼更快。

其实那句代码可以写得更简单一些:
for(int j=i+1;j<s.length;j++)
其它循环中也没有必要单独再定义一个变量去保存数组的长度

这是我改完之后的代码:

import java.math.BigDecimal;

public class MathTest {

public void test11(String[] s){
BigDecimal[] d = new BigDecimal[s.length];
for(int i=0;i<s.length;i++){
d[i] = new BigDecimal(s[i]);
}
for(int i=0;i<d.length;i++){
for(int j=i+1;j<s.length;j++){
if(d[i].compareTo(d[j])<0){
BigDecimal temp = d[i];
d[i] = d[j];
d[j] = temp;
}
}
}
for(int i=0; i<d.length;i++){
System.out.println(d[i]);
}
}

public static void main(String[] args) {
String[] s={"23414","1.3453","431241.3451531","432143.3412","432143.3413","432143.3411"};
new MathTest().test11(s);
}
}


以下是输出结果:

432143.3413
432143.3412
432143.3411
431241.3451531
23414
1.3453
meadking 2008-10-03
  • 打赏
  • 举报
回复
for循环的问题,呵呵
方法是对的
accp206 2008-10-03
  • 打赏
  • 举报
回复
你在写冒泡法排序时,内循环的for写错了。

原代码:
for(int j=i+1,len=s.length;i<len;i++)

把这句中的 i 改成 j 就对了。
cyazhang 2008-10-03
  • 打赏
  • 举报
回复
13行 for(int j=i+1,len=s.length;i<len;i++) 改为 for(int j=i+1,len=s.length;j<len;j++)
meadking 2008-10-03
  • 打赏
  • 举报
回复
public int compareTo(BigDecimal val)

将此 BigDecimal 与指定的 BigDecimal 比较。根据此方法,值相等但具有不同标度的两个 BigDecimal 对象(如,2.0 和 2.00)被认为是相等的。相对六个 boolean 比较运算符 (<, ==, >, >=, !=, <=) 中每一个运算符的各个方法,优先提供此方法。建议使用以下语句执行上述比较:(x.compareTo(y) <op> 0),其中 <op> 是六个比较运算符之一。

当此 BigDecimal 在数字上小于、等于或大于 val 时,返回 -1、0 或 1。

建议使用以下语句执行上述比较:(x.compareTo(y) <op> 0),其中 <op> 是六个比较运算符之一。
cyazhang 2008-10-03
  • 打赏
  • 举报
回复
先占个座
無名VF 2008-10-03
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 accp206 的回复:]
呵呵,还是3楼更快。

其实那句代码可以写得更简单一些:
for(int j=i+1;j <s.length;j++)
其它循环中也没有必要单独再定义一个变量去保存数组的长度

这是我改完之后的代码:

Java code
import java.math.BigDecimal;

public class MathTest {

public void test11(String[] s){
BigDecimal[] d = new BigDecimal[s.length];
for(int i=0;i<s.length;i++){
d[i] = new…
[/Quote]

UP
  • 打赏
  • 举报
回复
for(int j=i+1,len=s.length;j <len;j++)
accp206 2008-10-03
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 yeruping 的回复:]
读着读着API,
靠,竟然发现这几个类已经实现了Comparabe接口,我们的排序白干了哈哈
直接用这个
Arrays.sort(d);
搞定~~~~
[/Quote]

本来就是啊。还以为楼主是想自己练习一下冒泡排序算法呢。
yinsu 2008-10-03
  • 打赏
  • 举报
回复
接受学习呵呵
ThinkingInJava110 2008-10-03
  • 打赏
  • 举报
回复
也学习了!
yangkaixin1226 2008-10-03
  • 打赏
  • 举报
回复
for循环的问题
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 huoyin 的回复:]
也可以用Arrays.sort()来进行比较
Java codeimportjava.math.*;importjava.util.*;publicclassMathTest {publicstaticvoidmain(String[] args) {
String[] s={"23414","1.3453","431241.3451531","432143.3412","432143.3413","432143.3411"};
Arrays.sort(s,newComparator(){publicintcompare(Object o1, Object o2){returnnewBigDecimal(o1.toString()).compareTo(newBigDecimal(o2.toString()));

[/Quote]
想法不错……学习了……
Huhood 2008-10-03
  • 打赏
  • 举报
回复
呵呵,API是个好东西...得先学会怎么查.
ZangXT 2008-10-03
  • 打赏
  • 举报
回复
进步了。
yeruping 2008-10-03
  • 打赏
  • 举报
回复
读着读着API,
靠,竟然发现这几个类已经实现了Comparabe接口,我们的排序白干了哈哈
直接用这个
Arrays.sort(d);
搞定~~~~
加载更多回复(3)

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧