如何将用户输入存储在整数数组中? [英] How can I store user inputs in an array of integers?

查看:34
本文介绍了如何将用户输入存储在整数数组中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道用户将输入多少个数字,我需要程序在数组到达行尾时停止在数组中插入数字,因为在新行中,用户将输入一个与此无关的数字。

举例:

用户可以输入:

1 5 7 8 9 5

4

或:

%5%4%8%9%4%2%1%3%2%4

7

我需要的数组是,例如1:[1,5,7,8,9,5]

推荐答案

如果您的需求允许您询问用户,您可以这样做:

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));
    }
}

示例运行:

How many integers you want to enter: 3
Enter integer 1: 10
Enter integer 2: 15
Enter integer 3: 20
[10, 15, 20]

您也可以按如下方式操作:

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));
        }
    }
}

示例运行:

How many integers you want to enter: 3
Enter the integers separated by a space: 10 15 20    
[10, 15, 20]

但是,如果您希望整数数量不受限制,则应该使用List而不是数组(因为数组的大小在初始化时是固定的),如下所示:

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));
        }
    }
}

示例运行:

Enter the integers separated by a space: 10 20 30 40
[10, 20, 30, 40]
[10, 20, 30, 40]

祝您一切顺利!

这篇关于如何将用户输入存储在整数数组中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆