Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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_Arrays - Fatal编程技术网

java程序中的编译器错误

java程序中的编译器错误,java,arrays,Java,Arrays,这是我的第一篇帖子, 我现在正在写一个程序,我正在尝试交换数组中的两个索引,我相信我已经做对了,但是我遇到了一个编译器错误,我就是想不起来。如果有人能告诉我为什么我会出错,我将不胜感激。这是请求用户进行输入和错误检查的方法,效果很好 /////***************** Question 1 Part F (Method inside of Main) *****************\\\\\ System.out.print ("\n"); System.out.prin

这是我的第一篇帖子, 我现在正在写一个程序,我正在尝试交换数组中的两个索引,我相信我已经做对了,但是我遇到了一个编译器错误,我就是想不起来。如果有人能告诉我为什么我会出错,我将不胜感激。这是请求用户进行输入和错误检查的方法,效果很好

/////***************** Question 1 Part F (Method inside of Main)  *****************\\\\\    
System.out.print ("\n");
System.out.println ("Press 1 to edit the array: ");
int changeArray = keyIn.nextInt();

if(changeArray==1){
    //Ask to change indice 1
    System.out.println("Please enter the first indice you want to edit: (Between 0 & " + numbers.size() + ")");
    int changeFirstIndice = keyIn.nextInt();

    //Error check the first indice
    if (changeFirstIndice > numbers.size() || changeFirstIndice < 0) {
        System.out.println("ERROR!! You have entered an invalid number, please try again...");
    } //Ask to change indice 2
    else if (changeFirstIndice <= numbers.get(numbers.size() - 1) && changeFirstIndice > 0) {
        System.out.println("Please enter the second indice you want to edit: (Between 0 & " + numbers.size() + ")");
        int changeSecondIndice = keyIn.nextInt();

        //Error check the second indice
        if (changeSecondIndice > numbers.size() || changeSecondIndice < 0) {
            System.out.println("ERROR!! You have entered an invalid number, please try again...");
        } //Print the adjusted array
        else if (changeSecondIndice <= numbers.size() && changeFirstIndice > 0) {
            changeIndices(numbers, changeFirstIndice, changeSecondIndice);
            System.out.println(numbers);
        }
    }
}
这是给我错误的代码,对于我的生活来说,我看不出有什么错,这可能是一些简单的事情

public static ArrayList changeIndices(ArrayList<Integer> numbers, int changeFirstIndice, int changeSecondIndice) {
    int temp1 = numbers.get((changeFirstIndice) - 1);
    int temp2 = numbers.get((changeSecondIndice) - 1);

    //Swap the numbers
    numbers.set((changeFirstIndice - 1), temp2);
    numbers.set((changeSecondIndice - 1), temp1);

    return numbers;
}
[这些是我收到的错误]

因此,如前所述,我们非常感谢您的帮助: 以下是本部分涉及的所有代码:

/////***************** Question 1 Part F (Method inside of Main)  *****************\\\\\    
System.out.print("\n");
System.out.println("Press 1 to edit the array: ");
int changeArray = keyIn.nextInt();

if (changeArray == 1) {
    //Ask to change indice 1
    System.out.println("Please enter the first indice you want to edit: (Between 0 & " + numbers.size() + ")");
    int changeFirstIndice = keyIn.nextInt();

    //Error check the first indice
    if (changeFirstIndice > numbers.size() || changeFirstIndice < 0) {
        System.out.println("ERROR!! You have entered an invalid number, please try again...");
    }

    //Ask to change indice 2
    else if (changeFirstIndice <= numbers.get(numbers.size() - 1) && changeFirstIndice > 0) {
        System.out.println("Please enter the second indice you want to edit: (Between 0 & " + numbers.size() + ")");
        int changeSecondIndice = keyIn.nextInt();

        //Error check the second indice
        if (changeSecondIndice > numbers.size() || changeSecondIndice < 0) {
            System.out.println("ERROR!! You have entered an invalid number, please try again...");
        }

        //Print the adjusted array
        else if (changeSecondIndice <= numbers.size() && changeFirstIndice > 0) {
            changeIndices(numbers, changeFirstIndice, changeSecondIndice);

            System.out.println(numbers);
        }
    }
}

/////***************** Question 1 Part F (Method Outside of Main)  *****************\\\\\
public static ArrayList changeIndices(ArrayList < Integer > numbers, int changeFirstIndice, int changeSecondIndice) {
    int temp1 = numbers.get((changeFirstIndice) - 1);
    int temp2 = numbers.get((changeSecondIndice) - 1);

    //Swap the numbers
    numbers.set((changeFirstIndice - 1), temp2);
    numbers.set((changeSecondIndice - 1), temp1);

    return numbers;
}

因此,如前所述,我们将非常感谢您的帮助:

方法参数需要紧跟在方法名称后面,不带空格:

public static ArrayList changeIndices(ArrayList <Integer> numbers, int changeFirstIndice, int changeSecondIndice)

在声明方法public static ArrayList changeIndexes之前,代码末尾似乎没有}作为主方法的结尾


使用IDE,甚至像NotePad++这样的编程编辑器,可以帮助您进行大括号匹配。请在此处复制并粘贴这些错误。它不能被复制。这是一张图片。谢谢你的发帖。你能给我们changeIndex上面的4行左右吗?表达式的非法开始,直接指向方法的头行,几乎不可避免地意味着你缺少一个}或;在前面几行的某个地方。同样,你能把错误前面的四行左右的内容贴出来吗?这样我们就可以找到你的错误了?在相关代码可见的情况下,很难找到它们,如果没有这些代码,则更难找到它们。您的第一个问题可能是changeIndex的定义位于前一个方法中,而不是在其结束}之后。事实上,在上面的最新更新中,这显然是个问题。但可能还有其他问题。这完全是不真实的。在方法名和参数列表之间使用空格是完全有效的。如果我的编译器如此迂腐,我早就停止编程了。@hexafraction编译器基本上就是如此迂腐。@clwhisk这是惯例,不是规则。我可以用不同的方式在很多地方粘贴空白。我可以使用一个编译单元编写400类游戏,其中所有类都是静态嵌套类,并且都在一行上。编译器不会死。作为一个极端,我可以制作一个超过400行的hello world程序,大部分是空白。@clwhisk尝试一下,看看会发生什么。我刚刚意识到我本打算将该方法放在main之外,但在此之前并没有关闭main,所以Java Devil说我忘记关闭main是对的,非常感谢好先生:@user2880298感谢您提供有关问题的信息。很多时候,其他用户没有意识到什么是相关的,然后从来没有注意到要求额外信息的评论/采取行动。