Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么数组声明会出现语法错误?_Java - Fatal编程技术网

Java 为什么数组声明会出现语法错误?

Java 为什么数组声明会出现语法错误?,java,Java,我是Java新手;有人能给我解释一下为什么这会给我一个错误吗 说明资源路径位置类型 令牌“;”上的语法错误,{此令牌插入后应为sort.java/Alghoritems/src/sort行4 java问题 package sort; public class InsertionSort { int[] polje = { -2, 5, -14, 35, 16, 3, 25, -100 }; for(int firstUnsorted = 1; firstUnsorte

我是Java新手;有人能给我解释一下为什么这会给我一个错误吗

说明资源路径位置类型 令牌“;”上的语法错误,{此令牌插入后应为sort.java/Alghoritems/src/sort行4 java问题

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;
        
    }
    

}
包裹分拣;
公共类InsertionSort{
int[]polje={-2,5,14,35,16,3,25,100};
for(int firstUnsorted=1;firstUnsorted0&&polje[i-1]>newElement;i--){
}
}
对于(int i=0;i
在main方法中编写代码,那么错误就不会发生。

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

包裹分拣;
公共类InsertionSort{
公共静态void main(字符串[]args)
{
int[]polje={-2,5,14,35,16,3,25,100};
for(int firstUnsorted=1;firstUnsorted0&&polje[i-1]>newElement;i--){
}
}
对于(int i=0;i
每个Java应用程序都需要一个入口点,因此编译器知道从何处开始执行应用程序。对于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;
            
        }
        
    }    
}
包裹分拣;
公共类InsertionSort{
公共静态void main(字符串[]args){
int[]polje={-2,5,14,35,16,3,25,100};
for(int firstUnsorted=1;firstUnsorted0&&polje[i-1]>newElement;i--){
}
}
对于(int i=0;i
您的代码不在方法中这是否回答了您的问题?除了答案之外,我建议您在进行任何排序之前先学习该语言的基础知识。这对将来会很有帮助。
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;
            
        }
        
    }    
}