Welcome to Dream.In.Code
Become a Java Expert!

Join 149,479 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,491 people online right now. Registration is fast and FREE... Join Now!




Array Syntax

 
Reply to this topicStart new topic

Array Syntax, Syntax Structure

xosunkist
26 Apr, 2007 - 08:36 AM
Post #1

D.I.C Head
**

Joined: 28 Mar, 2007
Posts: 69


My Contributions
Is there a way this code could have been written better? Thanks. Any help.

CODE


/****************************************

* *

* This program will create two arrays.

* One will output the values of every

* number that is divisible  by 4 from 1-60.

* The other will output the numbers from

   1-60 evenly divisible by three, count

   the numbers and sum them.

***************************************/



public class ArrayTable

{

    public static void main(String[] args)

    {

        /*these are the two arrays that are need for

        the computations.*/



        int[] countByFour = new int[15];

        int[] countByThree = new int[20];



        int count;

        int loopCount=1;

        int fourNumber = 1;

        int threeNumber = 1;

        int sum = 0;

        int total=0;



        /*This loop will increment fourNumber by 4

        each time the loop is true and store that number in

        the array*/



        for (count = 0; count < 15; count++)

        {

            countByFour[count] = fourNumber * 4;





                                    if(loopCount<=4)

                                    {

                                                if((countByFour[count])<= 5)     // This is for spacing

                                                {                                              // on single digit numbers

                                                System.out.print(" ");



                                                }                 /*  end of second if block */



                                    System.out.print(countByFour[count]);

                                    System.out.print("    ");

                                     loopCount++;



                                     }                 /*  end of first if block */



                                    else

                                    {

                                    System.out.println("   ");

                                    System.out.print(countByFour[count]);

                                    System.out.print("   ");

                                    loopCount = 2;

                                    }                 /* end of else block */



            fourNumber++;



        }                                               /*  end of for block */



                        System.out.println("");

                        System.out.println("");



        /*This does the same thing as the loop above but

        instead it will store all the numbers in increments

        of three, count each number and store the sum of the

        numbers accumulated on each loop in a variable for output.*/



        loopCount = 1;



        for (count = 0; count < 20; count++)

                    {

                                    countByThree[count] = threeNumber * 3;





                                    if(loopCount<=4)

                                                {

                                                            if((countByThree[count])<=9)

                                                            {



                                                                        System.out.print(" ");  // This is for spacing on single digit numbers



}           /* end of second if block */      

            

System.out.print(countByThree[count]);

                                                            System.out.print("   ");

                                                            loopCount++;



                                                }                      /* end of first if block */



                                                            else

                                                            {

                                                            System.out.println("   ");

                                                            System.out.print(countByThree[count]);

                                                            System.out.print("   ");

                                                            loopCount = 2;

                                                            }  /* end of else block  */



                                                            sum = sum + countByThree[count];



                                                            total = total + 1;



                                    threeNumber++;



                        }                        /* end of for loop */







                        System.out.println("");

                        System.out.println("");





                        System.out.println("The number of numbers from 1-60 evenly divisible");

                        System.out.println("by 3 is " + total);



                        System.out.println("");

                        System.out.println("");





        /*The output of the sum of the numbers that are divisible by

        three from 1-60.*/







                        System.out.println("The sum of all the numbers from 1-60 evenly divisible");

                        System.out.println("by 3 is " + sum);







                        System.out.println("");

                        System.out.println("");







            }                      /*  end of main method  */



}                                    /*  end of  ArrayTable class */


This post has been edited by xosunkist: 26 Apr, 2007 - 08:37 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Array Syntax
26 Apr, 2007 - 09:57 AM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,348



Thanked: 51 times
Dream Kudos: 25
My Contributions
QUOTE

This loop will increment fourNumber by 4

you then have the code blow:
CODE

countByFour[count] = fourNumber * 4;

that is multiplying by four...incrementing by four would entail adding 4 to the value.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 04:06PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month