为什么我得到数组声明语法错误?



我是Java新手;有人能告诉我为什么这会给我一个错误吗?

Description资源路径位置类型insert . sort . Java/Alghoritems/src/sort line 4 Java Problem

package sort;
public class InsertionSort {
int[] polje = { -2, 5, -14, 35, 16, 3, 25, -100 };

for(int firstUnsorted = 1; firstUnsorted < polje.length; firstUnsorted++) {
int newElement = polje[firstUnsorted];
int i;
for (i = firstUnsorted; i > 0 && polje[i - 1] > newElement; i--) {

}
}

for (int i = 0; i < polje.length; i++){
int firstUnsorted = 1;
int elemant;

}

}

每个Java应用程序都需要一个入口点,这样编译器就知道从哪里开始执行应用程序。对于Java应用程序,您需要将代码封装在main()方法中。

public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World");
}
}
你的代码应该是
package sort;

public class InsertionSort {
public static void main (String[] args) {
int[] polje = { -2, 5, -14, 35, 16, 3, 25, -100 };

for(int firstUnsorted = 1; firstUnsorted < polje.length; firstUnsorted++) {
int newElement = polje[firstUnsorted];
int i;
for (i = firstUnsorted; i > 0 && polje[i - 1] > newElement; i--) {

}
}

for (int i = 0; i < polje.length; i++){
int firstUnsorted = 1;
int elemant;

}

}    
}

在main方法中编写代码,这样就不会出现错误。

首先,您必须了解Java是一种完全面向对象的编程语言。即使想做最简单的事情,也必须创建一个包含main方法的类。我建议你先从基础开始,然后再着手实现更困难的算法。

package sort;
public class InsertionSort {

public static void main(String[] args)
{
int[] polje = { -2, 5, -14, 35, 16, 3, 25, -100 };
for(int firstUnsorted = 1; firstUnsorted < polje.length; firstUnsorted++) {
int newElement = polje[firstUnsorted];
int i;
for (i = firstUnsorted; i > 0 && polje[i - 1] > newElement; i--) {
}
}
for (int i = 0; i < polje.length; i++){
int firstUnsorted = 1;
int elemant;
}
}
}

最新更新