Java 有人能帮我解决代码中的错误吗?
我已经在这方面工作了很长一段时间了,我对我所犯的错误感到沮丧。我希望有人能帮助我至少修复其中的一些错误,或者让我走上正确的方向。我的错误以**//开头。我已经试着查找我的问题,但找不到任何真正对我有帮助的东西,但我知道我会在这里找到一个可以帮助我的人,这就是我为什么发帖的原因Java 有人能帮我解决代码中的错误吗?,java,compiler-errors,Java,Compiler Errors,我已经在这方面工作了很长一段时间了,我对我所犯的错误感到沮丧。我希望有人能帮助我至少修复其中的一些错误,或者让我走上正确的方向。我的错误以**//开头。我已经试着查找我的问题,但找不到任何真正对我有帮助的东西,但我知道我会在这里找到一个可以帮助我的人,这就是我为什么发帖的原因 import java.util.*; import java.lang.Math; public class TestBoxBiggest { public static void main(String[]args
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]);
}
}
}```
import java.util.*;
导入java.lang.Math;
公共类测试盒
{
公共静态void main(字符串[]args)
{
//10个随机整数
随机n=新随机();
整数[]x=新整数(10);**//错误:无法从java.lang.Integer转换为java.lang.Integer[]**
对于(int i=0;i您的错误发生是因为您不理解Java中数组的概念,并且初始化它们的语法是错误的。请阅读文章以了解如何初始化数组。问题1。
Integer[] x= new Integer(10);**//error:cannot convert from java.lang.Integer to java.lang.Integer[]**
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**
Biggest.sort(names,names.length);//String implements comparable **//the method sort(java.lang.String[],int) is undefined for the type Biggest**
这里的问题是,您试图将非数组类型的对象newinteger(10)
分配给数组类型的变量Integer[]
正确的方法如下:
Integer[] x = new Integer[] { 10 };
Integer[] y = { 10 };
int[] z = { 10 };
问题2。
Integer[] x= new Integer(10);**//error:cannot convert from java.lang.Integer to java.lang.Integer[]**
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**
Biggest.sort(names,names.length);//String implements comparable **//the method sort(java.lang.String[],int) is undefined for the type Biggest**
这同样适用于下一个错误
Double[] d=new Double(10.0);**//cannot convert from java.lang.Double to java.lang.Double[]**
你需要
Double[] d = { 10.0 };
问题3。
Integer[] x= new Integer(10);**//error:cannot convert from java.lang.Integer to java.lang.Integer[]**
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**
Biggest.sort(names,names.length);//String implements comparable **//the method sort(java.lang.String[],int) is undefined for the type Biggest**
这里变量k
的类型为double
。数组需要int
类型作为索引。只需将k
的类型从double更改为int
另一个问题是Math.random()
不需要任何参数。只需调用它而不传递任何参数,它将返回0到1之间的随机值
问题4。
Integer[] x= new Integer(10);**//error:cannot convert from java.lang.Integer to java.lang.Integer[]**
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**
Biggest.sort(names,names.length);//String implements comparable **//the method sort(java.lang.String[],int) is undefined for the type Biggest**
如果不知道maxist.sort
method的签名,很难说确切的问题是什么?这是否回答了您的问题?新整数(10)创建一个值为10的整数对象。新整数[10]创建一个由10个整数对象组成的数组。