使用java反转数组
我在尝试反转数组时遇到问题,它会一直打印出来 这条信息可以帮助你找出我做错了什么使用java反转数组,java,arrays,Java,Arrays,我在尝试反转数组时遇到问题,它会一直打印出来 这条信息可以帮助你找出我做错了什么 Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.S
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at LabProgram.main(LabProgram.java:13)
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int[] userList = new int[20];
int numElements;
int i;
numElements = scnr.nextInt();
for (i = 0; i < userList.length; ++i) {
userList[i] = scnr.nextInt();
}
for (i = 0; i < userList.length/2; ++i) {
int temp = userList[i];
userList[i] = userList[userList.length -i -1];
userList[userList.length -i -1] = temp;
}
for (i = 0; i < userList.length; ++i) {
System.out.print(userList[i] + " ");
}
}
}
那么输出是:
10 8 6 4 2
下面是一个示例,请检查它
public class ReverseArray {
/* function that reverses array and stores it
in another array*/
static void reverse(int a[], int n)
{
int[] b = new int[n];
int j = n;
for (int i = 0; i < n; i++) {
b[j - 1] = a[i];
j = j - 1;
}
/*printing the reversed array*/
System.out.println("Reversed array is: \n");
for (int k = 0; k < n; k++) {
System.out.println(b[k]);
}
}
public static void main(String[] args)
{
int [] arr = {10, 20, 30, 40, 50};
reverse(arr, arr.length);
}
}
公共类反向数组{
/*反转数组并存储它的函数
在另一个数组中*/
静态无效反转(int a[],int n)
{
int[]b=新的int[n];
int j=n;
对于(int i=0;i
按以下步骤操作:
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.print("Enter the number of intgers: ");
int numElements = scnr.nextInt();
int[] userList = new int[numElements];
int i;
System.out.println("Enter " + numElements + " integers");
for (i = 0; i < userList.length; ++i) {
userList[i] = scnr.nextInt();
}
for (i = 0; i < userList.length / 2; ++i) {
int temp = userList[i];
userList[i] = userList[userList.length - i - 1];
userList[userList.length - i - 1] = temp;
}
for (i = 0; i < userList.length; ++i) {
System.out.print(userList[i] + " ");
}
}
}
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.print("Enter the number of intgers: ");
int numElements = scnr.nextInt();
int[] userList = new int[numElements];
int i;
System.out.println("Enter " + numElements + " integers");
for (i = 0; i < userList.length; ++i) {
userList[i] = scnr.nextInt();
}
for (i = 0; i < userList.length / 2; ++i) {
int temp = userList[i];
userList[i] = userList[userList.length - i - 1];
userList[userList.length - i - 1] = temp;
}
for (i = 0; i < userList.length; ++i) {
System.out.print(userList[i] + " ");
}
}
}
Enter the number of intgers: 6
Enter 6 integers
1
2
3
4
5
6
6 5 4 3 2 1