简体   繁体   中英

Why is my java program printing zero every time?

I am required to take an array of size x (which contains numbers starting at x and then descending down to 1), and then, with a new array of size y (which may or may not be the same size as x), print out random numbers from array x into array y. I wrote the program and it runs fine, but for some reason in the list of random numbers outputted, the number 0 will show up. Does anybody know why this is happening? Here is my code:

import java.util.Random;
public class Prog1A 
{
    public static void main(String[] args)
    {
        System.out.println("Program 1A, Christopher Moussa, masc1574");
        Random randGen = new Random();
        int[] arr_1 = new int[8];
        for (int i = arr_1.length - 1; i >= 0; i--)
        {
            arr_1[i] = arr_1[i];
        }
        int[] arr_2 = new int[6];
        for (int i = 1; i <= arr_2.length; i++)
        {
            System.out.print(randGen.nextInt(arr_1.length) + " ");
        }
    }
}

Any feedback will be greatly appreciated, thank you.

Well you aren't doing anything with this loop. You're essentially assigning a variable to itself right throughout the array. Also, I dislike the way you wrote your loop condition, but my preference isn't the issue here.

for (int i = arr_1.length - 1; i >= 0; i--)
    {
        arr_1[i] = arr_1[i]; //This code does nothing
    }

Then you create arr_2[] but you never assign anything to the variables. I went ahead and edited your code, and I'll explain a few things.

import java.util.Random;
public class Prog1A 
{
    public static void main(String[] args)
    {
        Random randGen = new Random();
        int[] arr_1 = new int[8];
        int[] arr_2 =  new int[6];

        System.out.println("Program 1A, Christopher Moussa, masc1574");

        //Assigns a random number to each member of arr_1
        for (int i = 0; i < arr_1.length; ++i)
        {
            arr_1[i] = randGen.nextInt(arr_1.length);
        }

        //Copies arr_1 values to arr_2
        for (int i = 0; i < arr_2.length; ++i) //Counting up [0 to 5]
        {
            arr_2[i] = arr_1[i];
        }

        for (int i = 0; i < arr_2.length; ++i)
        {
            System.out.print(arr_2[i] + " ");
        }
    }
}
  1. Always (if possible) declare all variables at the start of a function/class/program. It keeps code a lot cleaner and helps you to identify possible errors that may occur.

  2. Keep your loop parameters consistent. Start from 0 and go up always, or start from the last value and go down. It eliminates the possibility of an error again. I prefer starting from 0 always but it is up to you, as long as it is clean and it works.

  3. Unless you initialize an array, it is going to be empty. If you try to print from it you'll most likely see zeros.

Just add one to your result, The length of your array is 8. With your current code your are returning a random number between 0 and 7. The nextInt method will never return the upper limit of the integer value supplied. You can test this out yourself by exchanging the arr_1.length for a different number like 10 for example and then remove the + 1 . You will notice that it will only return the number 9 at the most and 0 will be the lowest number returned.

 System.out.print((randGen.nextInt(arr_1.length) + 1) + " ");

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