因此,我必須製作一個Java程序,其中用戶說他們要輸入多少值,在數組中輸入多個值,打印該數組。然後我必須顛倒數組中的元素(而不是反轉打印或創建一個新數組),並再次打印這些值。這是下面的代碼,我有:在int數組中重新整理int值java
package reversearray;
import java.util.*;
public class Swap_main {
/**
* Taylor Marino
*/
public static void main(String[] args) {
int arraysize = 0, junk, junk2;
Scanner reader = new Scanner(System.in);
System.out.println("How many values are you going to enter?");
arraysize = reader.nextInt();
int[] array = new int[arraysize];
System.out.println("You will now be asked to enter your values, one at a time");
for(int counter = 0; counter < arraysize; counter++){
System.out.println("Enter next value");
array[counter] = reader.nextInt();
}
System.out.println("The values you entered are: ");
for(int counter2 = 0; counter2 < arraysize; counter2++)
System.out.print(array[counter2] + ", ");
for(int counter3 = 0, counter4 = arraysize; counter3 != counter4; counter3++, counter4--){
junk = array[counter3];
junk2 = array[counter4];
array[counter4] = junk;
array[counter3] = junk2;
}
System.out.println("The values you entered are (in reverse order): ");
for(int counter5 = 0; counter5 < arraysize; counter5++)
System.out.print(array[counter5] + ", ");
}
}
但是我在這個循環中收到一個錯誤:
for(int counter3 = 0, counter4 = arraysize; counter3 != counter4; counter3++, counter4--){
junk = array[counter3];
junk2 = array[counter4];
array[counter4] = junk;
array[counter3] = junk2;
}
我不明白什麼是錯在這裏,但它說,有一個與array[counter4] = junk;
錯誤是什麼我做錯了嗎?
做錯的第一件事是不是說的錯誤是什麼:) –
什麼錯誤? –
@ user2033503。猜猜它在應用更改後現在對你有用 – user1760178