简体   繁体   中英

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. 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. Fix it by using the actual loop variable i like this:

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 . 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:

numArray[i] = in.nextInt();

2) If someone inputs a number that's bigger than the size of your array, you'll get an 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:-

1) Use i < n as you start from 0

2) Trying to add input value as index in the array.

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

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. 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. So the index is always one less than the array length. 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]);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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