简体   繁体   English

如何调整此代码以输入和打印数字列表?

[英]How Can this code be adjusted to input and print a list of numbers?

I'm trying to create a program that allows the user to input numbers into a array called "numArray", however an exception is thrown once the user inputs numbers into array. 我正在尝试创建一个程序,该程序允许用户将数字输入到名为“ numArray”的数组中,但是一旦用户将数字输入到数组中,就会引发异常。 How can this be fixed? 如何解决?

import java.util.Scanner;

class ArrayNumList
{
    public static void main(String[]args)
    {
        Scanner in = new Scanner(System.in);

        System.out.println("How many nums would you like to enter? ");
        int n = in.nextInt();
        System.out.println("Enter " + n + " nums");
        int[] numArray = new int[n];

        for(int i = 0; i <= n; i++)
        {
            int index = in.nextInt();
            System.out.print(numArray[index]);
        }
    }
}

There is no index index if the user inserts a big enough number / negative number. 如果用户插入足够大的数字/负数,则没有index索引。 Fix it by using the actual loop variable i like this: 通过实际的循环变量解决这个问题i是这样的:

for(int i = 0; i <= n; i++) {
    numArray[i] = in.nextInt();
    System.out.print(numArray[i]);
}

The problem is that you aren't storing your data anywhere in the array. 问题在于您没有将数据存储在阵列中的任何位置。

for(int i = 0; i <= n; i++)
{
    int index = in.nextInt();
    System.out.print(numArray[index]);
}

Every time you run through the array, you store your input into the variable index . 每次遍历数组时,都将输入存储到变量index From there, you try to display the value in your array at that index. 从那里,您尝试在该索引处显示数组中的值。 This can lead to a few problems. 这可能会导致一些问题。

1) You haven't put your numbers into this array yet, so each value in the array is going to be 0. To fix this: 1)您尚未将数字放入此数组中,因此数组中的每个值都将为0。要解决此问题:

numArray[i] = in.nextInt();

2) If someone inputs a number that's bigger than the size of your array, you'll get an IndexOutOfBoundsException . 2)如果有人输入的数字大于数组的大小,则会得到IndexOutOfBoundsException I don't think you were trying to do it this way, but this is what your code is currently attempting to do. 我不认为您试图以此方式进行操作,但这就是您的代码当前正在尝试进行的操作。

There are 3 issues:- 有3个问题:-

1) Use i < n as you start from 0 1)从0开始使用i <n

2) Trying to add input value as index in the array. 2)尝试将输入值添加为数组中的索引。

3) Printing out the values while entering which create confusion for you better to do that in separate loop but I leave it to u or Better to use println instead of print 3)在输入时打印出值,这会给您带来混乱,最好在单独的循环中完成,但是我还是把它留给了您,或者最好使用println而不是print

Try this:- 尝试这个:-

public static void main(String[]args) {
        Scanner in = new Scanner(System.in);

        System.out.println("How many nums would you like to enter? ");
        int n = in.nextInt();
        System.out.println("Enter " + n + " nums");
        int[] numArray = new int[n];

        for(int i = 0; i < n; i++)
        {

           numArray[i] = in.nextInt();
           System.out.println(numArray[i]);
        }

    }

Firstly it should be i<n and not i<=n because n means the array length and i means the array index. 首先,它应该是i<n而不是i<=n因为n表示数组长度,而i表示数组索引。 The index starts from 0. For eg an array holds the values 1234, so the length is 4 but the index are 0,1,2,3. 索引从0开始。例如,一个数组保留值1234,因此长度为4,但索引为0、1、2、3。 So the index is always one less than the array length. 因此索引总是比数组长度小1。 Also you have to store the input with the array index. 另外,您还必须将输入与数组索引一起存储。

for(int i = 0; i < n; i++)
    {

       numArray[i] = in.nextInt();
    }

And then later print it 然后再打印

for(int i = 0; i < n; i++)
    {

       System.out.println(numArray[i]);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM