これは私の最初の投稿なので、ここに行きます。現在、少しプログラムを書いており、現在、配列内の2つのインデックスを交換しようとしています。正しく行われたと信じていますが、コンパイルエラーが発生しています。私の頭の周り。エラーが発生する理由を誰かが教えてくれたら、大歓迎です。これは、ユーザーに入力とエラー チェックを求めるものであり、正常に動作します。
/////***************** 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;
}
[これらは私が得たエラーです] http://gyazo.com/4b3ccbb8316242a9335d5fb4b006dec0
したがって、前述のように、どんな助けも大歓迎です:)ここで質問する人々にとって、この部分に関係するすべてのコードは次のとおりです。
/////***************** 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;
}
したがって、前述のように、どんな助けも大歓迎です:)