p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Java Basics (http://p2p.wrox.com/forumdisplay.php?f=199)
-   -   Printing multiples of 13 using array (http://p2p.wrox.com/showthread.php?t=99804)

dTech March 11th, 2018 07:14 PM

Printing multiples of 13 using array
 
Hi, I'm new. I'm trying to use an array to print out multiples of 13, up to 400. (I know 400 isn't one of them.) All my computer prints out are zeros.

Here's my code:

Code:


class multiples {
    public static void main(String[] argumets){
        int mults [] = new int [400];
       
        for  (int dex = 0; dex < mults.length; dex++){
            int current = mults[dex];
            if (current % 13 == 0){
                System.out.println("#: " + current);
            }
        }
    }
}

Can someone tell me why it only prints zeros? Thanks in advance.

wasadeh March 20th, 2018 08:59 PM

Hi dTech,

You are mixing the concept of the array element index with the array element value.
The array mults has been created with capacity of 400 elements but all elements have been initialized to 0.
Your code simply is trying to pick the element values at index 13, 2*13,3*13,..and so on up to but excluding index 400. But the element values are already 0s.

Thanks,
--Wasadeh


All times are GMT -4. The time now is 12:59 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.