Java 如何将用户输入存储在整数数组中?
我不知道用户将输入多少个数字,我需要程序在到达行尾时停止在数组中插入数字,因为在新行中,用户将输入一个与此无关的数字 例如: 用户可以输入: 1577895 四, 或: 5 4 8 9 4 2 1 3 2 4 七,Java 如何将用户输入存储在整数数组中?,java,java.util.scanner,Java,Java.util.scanner,我不知道用户将输入多少个数字,我需要程序在到达行尾时停止在数组中插入数字,因为在新行中,用户将输入一个与此无关的数字 例如: 用户可以输入: 1577895 四, 或: 5 4 8 9 4 2 1 3 2 4 七, 我需要的数组是,例如1:[1,5,7,8,9,5]从我的头顶上: BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String value = reader.readLine(
我需要的数组是,例如1:[1,5,7,8,9,5]从我的头顶上:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String value = reader.readLine();
String[] strArr = value.split(" ");
int intArr[]= new int[strArr.length];
for(int i = 0; i < strArr.length; i++)
intArr[i] = Integer.parseInt(strArr[i]);
BufferedReader reader=新的BufferedReader(新的InputStreamReader(System.in));
字符串值=reader.readLine();
字符串[]strArr=value.split(“”);
int intArr[]=新int[strArr.length];
对于(int i=0;i
您可以使用列表保存号码。
如果确实需要数组类型变量。可以从列表中创建数组
List<Integer> list = Arrays.asList(1,2,3,4,5);
Integer[] array = list.toArray(new Integer[0]);
List List=Arrays.asList(1,2,3,4,5);
整数[]数组=list.toArray(新整数[0]);
我同意您应该询问用户数组的大小,因为数组是静态数据结构。
使用链表数据结构。如果您的需求允许您询问用户,您可以按如下方式执行:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] num;
Scanner scanner=new Scanner(System.in);
System.out.print("How many integers you want to enter: ");
int n = 0;
if(scanner.hasNextInt()) {
n=scanner.nextInt();
}
num=new int[n];
for(int i=0;i<n;i++) {
System.out.printf("Enter integer %d: ",i+1);
if(scanner.hasNextInt()) {
num[i]=scanner.nextInt();
}
}
System.out.println(Arrays.toString(num));
}
}
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] num;
Scanner scanner = new Scanner(System.in);
System.out.print("How many integers you want to enter: ");
int n = 0;
if (scanner.hasNextInt()) {
n = scanner.nextInt();
scanner.nextLine();
}
num = new int[n];
System.out.print("Enter the integers separated by a space: ");
String[] strNums = null;
if (scanner.hasNextLine()) {
strNums = scanner.nextLine().split(" ");
}
if (strNums != null) {
for (int i = 0; i < n; i++) {
try {
num[i] = Integer.parseInt(strNums[i]);
} catch (Exception e) {
System.out.println("Invalid input");
break;
}
}
System.out.println(Arrays.toString(num));
}
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
List <Integer> intList=new ArrayList<Integer>();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the integers separated by a space: ");
String[] strNums = null;
if (scanner.hasNextLine()) {
strNums = scanner.nextLine().split(" ");
}
if (strNums != null) {
for (String strNum: strNums) {
try {
intList.add(Integer.parseInt(strNum.trim()));
} catch (Exception e) {
System.out.println("Invalid input");
break;
}
}
System.out.println(intList);
//You can even get an array out of the list as follows:
Integer[] nums = intList.toArray(new Integer[0]);
System.out.println(Arrays.toString(nums));
}
}
}
您也可以按如下方式执行此操作:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] num;
Scanner scanner=new Scanner(System.in);
System.out.print("How many integers you want to enter: ");
int n = 0;
if(scanner.hasNextInt()) {
n=scanner.nextInt();
}
num=new int[n];
for(int i=0;i<n;i++) {
System.out.printf("Enter integer %d: ",i+1);
if(scanner.hasNextInt()) {
num[i]=scanner.nextInt();
}
}
System.out.println(Arrays.toString(num));
}
}
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] num;
Scanner scanner = new Scanner(System.in);
System.out.print("How many integers you want to enter: ");
int n = 0;
if (scanner.hasNextInt()) {
n = scanner.nextInt();
scanner.nextLine();
}
num = new int[n];
System.out.print("Enter the integers separated by a space: ");
String[] strNums = null;
if (scanner.hasNextLine()) {
strNums = scanner.nextLine().split(" ");
}
if (strNums != null) {
for (int i = 0; i < n; i++) {
try {
num[i] = Integer.parseInt(strNums[i]);
} catch (Exception e) {
System.out.println("Invalid input");
break;
}
}
System.out.println(Arrays.toString(num));
}
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
List <Integer> intList=new ArrayList<Integer>();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the integers separated by a space: ");
String[] strNums = null;
if (scanner.hasNextLine()) {
strNums = scanner.nextLine().split(" ");
}
if (strNums != null) {
for (String strNum: strNums) {
try {
intList.add(Integer.parseInt(strNum.trim()));
} catch (Exception e) {
System.out.println("Invalid input");
break;
}
}
System.out.println(intList);
//You can even get an array out of the list as follows:
Integer[] nums = intList.toArray(new Integer[0]);
System.out.println(Arrays.toString(nums));
}
}
}
但是,如果希望整数的数量不受限制,则应使用列表
而不是数组(因为数组的大小在初始化时是固定的),如下所示:
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] num;
Scanner scanner=new Scanner(System.in);
System.out.print("How many integers you want to enter: ");
int n = 0;
if(scanner.hasNextInt()) {
n=scanner.nextInt();
}
num=new int[n];
for(int i=0;i<n;i++) {
System.out.printf("Enter integer %d: ",i+1);
if(scanner.hasNextInt()) {
num[i]=scanner.nextInt();
}
}
System.out.println(Arrays.toString(num));
}
}
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] num;
Scanner scanner = new Scanner(System.in);
System.out.print("How many integers you want to enter: ");
int n = 0;
if (scanner.hasNextInt()) {
n = scanner.nextInt();
scanner.nextLine();
}
num = new int[n];
System.out.print("Enter the integers separated by a space: ");
String[] strNums = null;
if (scanner.hasNextLine()) {
strNums = scanner.nextLine().split(" ");
}
if (strNums != null) {
for (int i = 0; i < n; i++) {
try {
num[i] = Integer.parseInt(strNums[i]);
} catch (Exception e) {
System.out.println("Invalid input");
break;
}
}
System.out.println(Arrays.toString(num));
}
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
List <Integer> intList=new ArrayList<Integer>();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the integers separated by a space: ");
String[] strNums = null;
if (scanner.hasNextLine()) {
strNums = scanner.nextLine().split(" ");
}
if (strNums != null) {
for (String strNum: strNums) {
try {
intList.add(Integer.parseInt(strNum.trim()));
} catch (Exception e) {
System.out.println("Invalid input");
break;
}
}
System.out.println(intList);
//You can even get an array out of the list as follows:
Integer[] nums = intList.toArray(new Integer[0]);
System.out.println(Arrays.toString(nums));
}
}
}
祝你一切顺利 到目前为止你试过什么?你能给我们看一些代码吗?如果你不知道要存储多少个数字,你应该使用
ArrayList
而不是int[]
。数组不是动态的。这意味着它们在运行时的大小是固定的。这意味着,如果您不总是要求用户提供相同数量的项目,那么这就不是理想的数据结构。查看java数组列表,我认为这个问题特别要求用户不输入他们输入的整数数量。在回答中,您要求用户输入该值。如果我们知道价值观的数量,我们就不会问这个问题。谢谢,你发布的第三个程序成功了