您好我是初学者,今天我开始学习数组。我写了以下工作程序。
import java.util.Scanner;
public class Arr {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("enter some numbers");
int[] x = new int[5];
x[0] = scanner.nextInt();
x[1] = scanner.nextInt();
x[2] = scanner.nextInt();
x[3] = scanner.nextInt();
x[4] = scanner.nextInt();
String string = "the numbers as requested are : ";
for(int i = 0; i < x.length; i++) {
System.out.println(string + x[i]);
}
scanner.close();
}
}
然而,如果我的阵列有1000个数字,这个过程就会变得很烦人。是否有一种简单的方法可以在不输入input scanner for each package
答案 0 :(得分:1)
不要只是硬编码。使用while循环并使用限制。
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("enter some numbers");
int[] x = new int[5];
int j = 0;
while (scanner.hasNext() && j < limit) {
if (scanner.hasNextInt()) {
x[j] = scanner.nextInt();
j++;
}
}
String string = "the numbers as requested are : ";
for (int i = 0; i < x.length; i++) {
System.out.println(string + x[i]);
}
scanner.close();
}
4
输入的限制为5
,99
输入的限制为100
。
答案 1 :(得分:1)
然而,如果我的阵列有1000个数字,这个过程就会变得很烦人。是否有一种简单的方法来输入数字而无需为每个包输入输入扫描器
是的,通过使用循环。使用 for-loop 或 while循环。
int[] array = new int[5];
for(int x=0; x<array.length; x++) //Prompt as many times as the array size
array[x] = scanner.nextInt();
一般情况下,当您确定要迭代的次数时,使用 for循环,当您不确定循环次数时,使用 while循环运行
答案 2 :(得分:0)
扩展@Suresh-Atta提及的没有硬编码限制的想法我会考虑使用ArrayList
,请参阅Why is it preferred to use Lists instead of Arrays in Java来存储动态数量的项目。这样做意味着您需要一些指示符来退出输入上的读数,在这种情况下,单词“exit”
import java.util.Scanner;
import java.util.ArrayList;
public class Arr {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("enter some numbers");
ArrayList<Integer> numbers = new ArrayList<Integer>();
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
numbers.add(scanner.nextInt());
} else {
String s1 = scanner.next();
if ("exit".equalsIgnoreCase(s1)) {
break;
}
}
}
String string = "the numbers as requested are : ";
for (Integer x : numbers) {
System.out.println(x);
}
scanner.close();
}
}
答案 3 :(得分:0)
Here's a sample:
public static void main(String[] args) {
int ARRAY_LENGTH = 5;
int[] intArray = new int[ARRAY_LENGTH];
int x = 0;
try (Scanner reader = new Scanner(System.in)) {
System.out.println("Enter " + ARRAY_LENGTH + " numbers [Something else exits]: ");
while (x < ARRAY_LENGTH) {
if (reader.hasNextInt()) {
intArray[x++] = reader.nextInt();
} else {
System.out.println("Input not a number, exiting.");
break;
}
}
}
//Print array
System.out.print("Array has: ");
for (int number : intArray) {
System.out.print(number + " ");
}
System.out.println();
}
Here's another:
public static void main(String[] args) {
ArrayList<Integer> array = new ArrayList();
try (Scanner reader = new Scanner(System.in)) {
System.out.println("Enter numbers [Something else exits]: ");
while (reader.hasNextInt()) {
array.add(reader.nextInt());
}
}
//Print array
System.out.print("Array has: ");
for (int number : array) {
System.out.print(number + " ");
}
System.out.println();
}