[英]Order array of type float in descendent order java
I am a newbie in Java. 我是Java的新手。 I need order a array of type float in order descendent, but my code seems not working
我需要为后代订购一个float类型的数组,但是我的代码似乎无法正常工作
package java1;
import java.util.Arrays;
import java.util.Collections;
public class Orden{
public static void main(String[] args) {
float []nums={23.6,35,5,4,4,3,10.9,2,45,56};
Float[]floatArray=new Float[nums.length];
for (int i = 0; i < nums.length; i++) {
floatArray[i]=nums[i];
}
Arrays.sort(floatArray,Collections.reverseOrder());
for (int i = 0; i < floatArray.length; i++) {
System.out.println("num:"+floatArray[i]);
}
}
}
The key is to pass the float numbers to the float array. 关键是将浮点数传递给float数组。 ie to specify
f
to the values of the numbers. 即指定
f
为数字的值。
float[] nums = {23.6f, 35f, 5f, 4f, 4f, 3f, 10.9f, 2f, 45f, 56f};
Try this.. this works. 试试这个..这有效。
float[] nums = {23.6f, 35f, 5f, 4f, 4f, 3f, 10.9f, 2f, 45f, 56f};
Float floatArray[] = new Float[nums.length];
for (int i = 0; i < nums.length; i++) {
floatArray[i] = nums[i];
}
Arrays.sort(floatArray, Collections.reverseOrder());
for (int i = 0; i < floatArray.length; i++) {
System.out.println("num:" + floatArray[i]);
}
我运行了您的代码,并在将23.6和10.9声明为float之后,对我来说效果很好。
float []nums={23.6f,35,5,4,4,3,10.9f,2,45,56};
You should append f to each constant number with a decimal point to make Java compiler read it like a float. 您应该在每个常量数字后加上小数点f,以使Java编译器像浮点数一样读取它。
If you don't, it will read it as a double. 如果您不这样做,它将被读取为双精度。
You choices are: 您的选择是:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.