Cazador de sombras :
Estoy teniendo problemas tratando de matrices inversas y se mantiene en la impresión de que este mensaje puede ayudar a saber lo que estoy haciendo mal.
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] + " ");
}
}
}
ejemplo si la entrada es:
5 2 4 6 8 10
entonces la salida es:
10 8 6 4 2
Arvind Kumar Avinash:
Hacerlo de la siguiente manera:
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] + " ");
}
}
}
Un análisis de la muestra:
Enter the number of intgers: 6
Enter 6 integers
1
2
3
4
5
6
6 5 4 3 2 1