在eclipse里可以运行,但是放到OJ里就报错
运行错误:
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 Main.randomGeneratorArray(Main.java:33)
at Main.main(Main.java:92)
原代码
import java.util.Scanner;
public class develop_1165 {
public static void bubbleSort(int[] arr) {
for (int i = arr.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}
}
public static void swap(int[] arr, int i, int j) {
arr[i] = arr[i] ^ arr[j];
arr[j] = arr[i] ^ arr[j];
arr[i] = arr[i] ^ arr[j];
}
public static void displayArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
public static int[] randomGeneratorArray(int value, int size) {
int[] arr = new int[size];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < arr.length; i++) {
int k = sc.nextInt();
if (k <= value && k > 0) {
arr[i] = k;
}
}
return arr;
}
public static int[] againArray(int[] arr1) {
int[] arr2 = copyArray(arr1);
for (int i = arr1.length - 1; i > 0; i--) {
for (int j = 0; j < i; j++) {
if (arr1[j] == arr1[i]) {
arr2 = deleteArray(arr2, i);
}
}
}
return arr2;
}
public static int[] deleteArray(int[] arr2, int num) {
int[] arr3 = new int[arr2.length - 1];
if (num < arr2.length) {
for (int i = 0; i < num; i++) {
arr3[i] = arr2[i];
}
for (int i = num + 1, j = num; i < arr2.length && num < arr3.length; i++, j++) {
arr3[j] = arr2[i];
}
}
if (num <= arr2.length) {
return arr2;
}
return arr3;
}
public static int[] copyArray(int[] arr1) {
if (arr1 == null) {
return null;
}
int[] arr2 = new int[arr1.length];
for (int i = 0; i < arr1.length; i++) {
arr2[i] = arr1[i];
}
return arr2;
}
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
int value = 1000;
int size;
if (i > 0 && i <= 100) {
size = i;
int[] arr1 = randomGeneratorArray(value, size);
System.out.println();
int[] arr2 = againArray(arr1);
bubbleSort(arr2);
System.out.println(arr2.length);
displayArray(arr2);
}
}