java - 有人可以帮我解决我的代码错误吗?

标签 java compiler-errors

这个问题在这里已经有了答案:





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/

相关文章:

java - 找不到 C :\Development\android-sdk-windows\tools\emulator. exe?

ios - Xcode 编译意外结果

makefile - 如何使用 cmake 链接 jemalloc 共享库

java - 转换HH :MM:SS (AM/PM) into Seconds的String时间格式

java - GWT - 编译错误 : No source code is available for type

java - 由于其他项目中未找到导入,Ant 构建失败

java - Android NDK 库仅在运行时不加载三星 galaxy 4.0.2 手机

java - 如何避免使用 Spring-Boot 下载嵌入式 MongoDb

java - 计算谢尔宾斯基三角形中三角形的数量

C风格宏编译