这个问题在这里已经有了答案:
How to initialize an array in Java?
(10 个回答)
2年前关闭。
我已经为此工作了很长一段时间,并且对我遇到的错误感到沮丧,我希望有人可以帮助我至少解决其中的一些问题,或者让我朝着正确的方向前进。我的错误以 **//开头。我已经尝试查找我的问题,但找不到任何真正帮助我的东西,但我知道我会在这里找到能做到的人,这就是我发帖的原因。
import java.util.*;
import java.lang.Math;
public class TestBoxBiggest
{
public static void main(String[]args)
{
//10 random Integers
Random n=new Random();
Integer[] x= new Integer(10);**//error:cannot convert from java.lang.Integer to java.lang.Integer[]**
for (int i=0;i<10;i++)
{
x[i]=n.nextInt(100);
System.out.println("The Integers are :\n"+x[i]);
}
SelectionSort.sort(x,10);
System.out.println("The greatest Integer is:" +x);
//10 random Doubles\
Double[] d=new Double(10.0);**//cannot convert from java.lang.Double to java.lang.Double[]**
for (double k=0.0;k<10.0;k++)
{
d[k]=Math.random(1.0);**//cannot convert from double to int & the method random() in the type java.lang.Math is not applicable for the arguments (double)**
System.out.println("The Doubles are:\n"+d[k]);**//cannot convert from double to int**
}
SelectionSort.sort(d,10);
System.out.println("The greatest Double is:" +d);
//5 Random box objects
Random r=new Random();
Box[]b=new Box[5];
for (int i=0;i<5;i++)
{
int length=r.nextInt(30)+1;
int width=r.nextInt(30)+1;
int height=r.nextInt(30)+1;
b[i]=new Box(length,width,height);
System.out.println("The boxes are: "+b[i]);
}
SelectionSort.sort(b,5);
System.out.println("Boxes sorted by volume:\n");
for (int i=0;i<5;i++)
{
System.out.println(b[i]+ "Volume: "+b[i].volume());
}
//5 String names
String[] names={"Bobby","Freddie","John","Ralph","Usman"};
System.out.println("The Strings are: "+names);
Biggest.sort(names,names.length);//String implements comparable **//the method sort(java.lang.String[],int) is undefined for the type Biggest**
System.out.println("Sorted Strings\n");
for(int i=0;i<names.length;i++)
{
System.out.println(names[i]);
}
}
}```
最佳答案
发生错误是因为您不了解 Java 中数组的概念,并且初始化它们的语法是错误的。阅读 this发布以了解如何初始化数组。
关于java - 有人可以帮我解决我的代码错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60765666/