简体   繁体   中英

how do i get rid of the zero that shows up after all my elements in my array are listed?

i have to make a program that prints out the elements of an array filled with random numbers backwards but it also prints out a zero after my variables? i think it has something to do with my for-loops but i get a java.lang.ArrayIndexOutOfBoundsException error any time i try to change them.

edit: i also noticed that the first printed element is always zero. i don't really see it as a problem but what could be causing that?

import java.util.*;

public class EZD_printBackwards
{
    static int vars[] = new int[10];

    static int backwards()
    { 
        Random rand = new Random();
        int bkwds = vars[0];

        for (int a = 0; a < 10; a++) 
        {
            vars[a] = rand.nextInt(100)+1;
        }

        for (int x = 10; x > 0; x--)
        {
            System.out.println("Element " + x + " is " + vars[x] );      
        }
        return bkwds;
    }

    public static void main(String Args[])
    {

       int option;
       Scanner kbReader = new Scanner(System.in);

       do
       {
           System.out.println(backwards());
           System.out.print("Type 1 to run again. Type 0 to end the program.");
           option = kbReader.nextInt();

           while(option !=1 && option !=0)
           {

               if (option != 1 && option != 0)
               {
                   System.out.print("Please enter 1 to run again or 0 to end the program.");
                   option = kbReader.nextInt();
               }
           }

       }while(option==1);

       System.out.println("");
    }
}

In your example, vars is size 10. The second for loop starts at index x = 10 which is greater than the last index of vars because array index starts at zero. To fix the issue, you should use this condition in your for loop:

 for (int x = vars.length -1; x >= 0; x--)
 {
  System.out.println("Element " + x + " is " + vars[x] );      
 }

You have two problems:

FIRST => For loop out of bounds

you want to go through 10 elements, therefore your

for (int x = 10; x > 0; x--)

should be

for (int x = 9; x >= 0; x--)

0..9 is 10 elements and the same things goes for

for (int a = 0; a < 10; a++) 

to

for (int a = 0; a < 9; a++)

SECOND ==> 0 at the end

That's because you do

System.out.println(backwards());

instead of

backwards();

you are returning the value of "bkwds", and you are initializing "bkwds" with vars[0] , and i guess in java the compiler initialize all the values of an array with 0 , in other words your probleme is in System.out.println(backwards());

remove it with

backwards();

and it should work !

EDIT

your look is out of bound either replace it with this

        for (int a = 0; a < 10; a++)

or do this

        for (int a = 9; a > 0; a--)

As arrays are indexed from zero, change to

System.out.println("Element " + x + " is " + vars[x-1] );    

output

Element 10 is 31
Element 9 is 63
Element 8 is 82
Element 7 is 46
Element 6 is 67
Element 5 is 24
Element 4 is 3
Element 3 is 27
Element 2 is 37
Element 1 is 13

edit

And change this

System.out.println(backwards());

to

backwards();

As the return value of this method is useless and not used, change this method to return void

The range of your Array is from 0 up to and including 9. Your for loop, however, is starting from 10 and coming down to index 1. It should start from 9 and comes down to index zero. This, however, does not explain printing random 0! The reason for these zeros is you have an int bkwds which youb set to vars[0] at the beginning of your function. But, since it is not initialized, it will be set to 0 (the default for int) In your main, you call the function in a print statement, which I suppose keeps printing that falsely, useless initialized variable that you return :)

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