Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: help with this, just a question

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default help with this, just a question

    ok we just started arrays yesterday and made us do a program..
    this basically prints 2 lists of arrays that has 10 blocks with random numbers in each block (1-99)
    it works but does anyone know how to make this code better? any suggestions?
    its kind of ugly because of all the if conditionals though...

    int x, y =0;
    int [] list;
    list = new int [10];
    int [] list2;
    list2 = new int [10];
    x =(int)(99*Math.random()+1);
    y=(int)(99*Math.random()+1);
    int array = 9;
    int [] list3;
    list3 = new int [10];
    int [] list4;
    list4 = new int [10];
     
     
     
     
    list[0] = x;
    x =(int)(99*Math.random()+1);
    list[1] = x;
    x =(int)(99*Math.random()+1);
    list[2] = x;
    x =(int)(99*Math.random()+1);
    list[3] = x;
    x =(int)(99*Math.random()+1);
    list [4] = x;
    x =(int)(99*Math.random()+1);
    list [5] = x;
    x =(int)(99*Math.random()+1);
    list [6] = x;
    x =(int)(99*Math.random()+1);
    list [7] = x;
    x =(int)(99*Math.random()+1);
    list [8] = x;
    x =(int)(99*Math.random()+1);
    list [9] = x;
    x =(int)(99*Math.random()+1);
     
    list2[0] = x;
    x =(int)(99*Math.random()+1);
    list2[1] = x;
    x =(int)(99*Math.random()+1);
    list2[2] = x;
    x =(int)(99*Math.random()+1);
    list2[3] = x;
    x =(int)(99*Math.random()+1);
    list2[4] = x;
    x =(int)(99*Math.random()+1);
    list2[5] = x;
    x =(int)(99*Math.random()+1);
    list2[6] = x;
    x =(int)(99*Math.random()+1);
    list2[7] = x;
    x =(int)(99*Math.random()+1);
    list2[8] = x;
    x =(int)(99*Math.random()+1);
    list2[9] = x;
    x =(int)(99*Math.random()+1);
     
     
     
     
     
     System.out.println("List 1 = "  + list[0] + " " +list[1] + " " + list[2] + " " 
             + list[3]+" " + list[4] + " " +list[5] + " " + list[6] + " " + list[7]
             + " "+ list[8] + " " + list[9]);
     
     System.out.println("List 2 = "  + list2[0] + " " +list2[1] + " " + list2[2] + " "
             + list2[3]+" " + list2[4] + " " +list2[5] + " " + list2[6] + " " + list2[7]
            + " "+ list2[8] + " " + list2[9]);
     
     
     
    if(list[0]  > list2[0])
     list3[0] = list[0];
     else
         list3[0] = list2[0];
     
     
     if(list[1]  > list2[1])
     list3[1] = list[1];
     else
         list3[1] = list2[1];
     
     if(list[2]  > list2[2])
     list3[2] = list[2];
     else
         list3[2] = list2[2];
     
     if(list[3]  > list2[3])
     list3[3] = list[3];
     else
         list3[3] = list2[3];
     
    if(list[4]  > list2[4])
     list3[4] = list[4];
     else
         list3[4] = list2[4];
     
     if(list[5]  > list2[5])
     list3[5] = list[5];
     else
         list3[5] = list2[5];
     
     if(list[6]  > list2[6])
     list3[6] = list[6];
     else
         list3[6] = list2[6];
     
     if(list[7]  > list2[7])
     list3[7] = list[7];
     else
         list3[7] = list2[7];
     
     
     if(list[8]  > list2[8])
     list3[8] = list[8];
     else
         list3[8] = list2[8];
     
     
     if(list[9]  > list2[9])
     list3[9] = list[9];
     else
         list3[9] = list2[9];
     
     
     
     
     
     if(list[0]  < list2[0])
     list4[0] = list[0];
     else
         list4[0] = list2[0];
     
     
     if(list[1]  < list2[1])
     list4[1] = list[1];
     else
         list4[1] = list2[1];
     
     if(list[2]  < list2[2])
     list4[2] = list[2];
     else
         list4[2] = list2[2];
     
     if(list[3]  < list2[3])
     list4[3] = list[3];
     else
         list4[3] = list2[3];
     
    if(list[4]  < list2[4])
     list4[4] = list[4];
     else
         list4[4] = list2[4];
     
     if(list[5]  < list2[5])
     list4[5] = list[5];
     else
         list4[5] = list2[5];
     
     if(list[6]  < list2[6])
     list4[6] = list[6];
     else
         list4[6] = list2[6];
     
     if(list[7]  < list2[7])
     list4[7] = list[7];
     else
         list4[7] = list2[7];
     
     
     if(list[8]  < list2[8])
     list4[8] = list[8];
     else
         list4[8] = list2[8];
     
     
     if(list[9]  < list2[9])
     list4[9] = list[9];
     else
         list4[9] = list2[9];
     
     
    System.out.println();
    System.out.println("List 3 = "  + list3[0] + " " +list3[1] + " " + list3[2] + " "
             + list3[3]+" " + list3[4] + " " +list3[5] + " " + list3[6] + " " + list3[7]
            + " "+ list3[8] + " " + list3[9]);
     
    System.out.println("List 4 = "  + list4[0] + " " +list4[1] + " " + list4[2] + " "
             + list4[3]+" " + list4[4] + " " +list4[5] + " " + list4[6] + " " + list4[7]
            + " "+ list4[8] + " " + list4[9]);
     
     
     
     
     
        }
    }
    Last edited by helloworld922; November 11th, 2010 at 09:38 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: help with this, just a question

    Quote Originally Posted by gonfreecks View Post
    ok we just started arrays yesterday and made us do a program..
    this basically prints 2 lists of arrays that has 10 blocks with random numbers in each block (1-99)
    it works but does anyone know how to make this code better? any suggestions?
    its kind of ugly because of all the if conditionals though...




     
    // have updated it slightly
    int x = 0;
    int y =0;
    Integer [] list;
    list = new Integer[10];
    Integer [] list2;
    list2 = new Integer [10];
    x =(int)(99*Math.random()+1);
    y=(int)(99*Math.random()+1);
    int array = 9;
    Integer [] list3;
    list3 = new Integer [10];
    Integer [] list4;
    list4 = new Integer [10];
     
     
     
    // this makes list[0] = value you gave it above
    list[0] = x;
    x =(int)(99*Math.random()+1);
    // this makes list[1] = value for new x above
    list[1] = x;
    x =(int)(99*Math.random()+1);
    // this makes list[2] = value for new x above
    list[2] = x;
    x =(int)(99*Math.random()+1);
    // this makes list[3] = value for new x above
    list[3] = x;
    x =(int)(99*Math.random()+1);
    // this makes list[4] = value for new x above
    list [4] = x;
    x =(int)(99*Math.random()+1);
    // this makes list[5] = value for new x above
    list [5] = x;
    x =(int)(99*Math.random()+1);
    // this makes list[6] = value for new x above
    list [6] = x;
    x =(int)(99*Math.random()+1);
    // this makes list[7] = value for new x above
    list [7] = x;
    x =(int)(99*Math.random()+1);
    // this makes list[8] = value for new x above
    list [8] = x;
    x =(int)(99*Math.random()+1);
    // this makes list[9] = value for new x above.
    list [9] = x;
    x =(int)(99*Math.random()+1);
    // this sets list2[0] to value for x above, is this what you wanted?  
    list2[0] = x;
    x =(int)(99*Math.random()+1);
    list2[1] = x;
    x =(int)(99*Math.random()+1);
    list2[2] = x;
    x =(int)(99*Math.random()+1);
    list2[3] = x;
    x =(int)(99*Math.random()+1);
    list2[4] = x;
    x =(int)(99*Math.random()+1);
    list2[5] = x;
    x =(int)(99*Math.random()+1);
    list2[6] = x;
    x =(int)(99*Math.random()+1);
    list2[7] = x;
    x =(int)(99*Math.random()+1);
    list2[8] = x;
    x =(int)(99*Math.random()+1);
    list2[9] = x;
    x =(int)(99*Math.random()+1);
     
     
            // for lines below, you could always make either change the values from int x to Integer x above and make it an array of Integers, or copy it into Integer array.
           // Integer class has a toString() method.  
    String[] str = new String[list.length];
    String str2 = "";
    for (int v = 0; v < list.length; v++)
    {
    if (v ==0)
    str[v] = list[v];
     
    else 
    str[v] = str[v-1] + " " + list[v].toString();
     
    if (v == list.length -1)
    str2 = str[v];
     
    }
     
            System.out.println("List 1 = " + str2);
     
    /*
     System.out.println("List 1 = "  + list[0] + " " +list[1] + " " + list[2] + " " 
             + list[3]+" " + list[4] + " " +list[5] + " " + list[6] + " " + list[7]
             + " "+ list[8] + " " + list[9]);
     
    */
     
    String[] str4 = new String[list2.length];
    String str3 = "";
    for (int w = 0; w < list2.length; w++)
    {
    if (w ==0)
    str4[w] = list2[w];
     
    else 
    str4[w] = str4[w-1] + " " + list2[w].toString();
     
    if (w == list2.length -1)
    str3= str4[w];
     
    }
     
            System.out.println("List 2 = " + str3);
     
    /*
     System.out.println("List 2 = "  + list2[0] + " " +list2[1] + " " + list2[2] + " "
             + list2[3]+" " + list2[4] + " " +list2[5] + " " + list2[6] + " " + list2[7]
            + " "+ list2[8] + " " + list2[9]);
    */
     
    // this will work since list and list2 and list3 are all same length
    for (int k =0; k < list.length; k++)
    {
     
    if (list[k] > list2[k])
    list3[k] = list[k];
     
    else
    list3[k] = list2[k];
    }
     
    /*
    if(list[0]  > list2[0])
     list3[0] = list[0];
     else
         list3[0] = list2[0];
     
     
     if(list[1]  > list2[1])
     list3[1] = list[1];
     else
         list3[1] = list2[1];
     
     if(list[2]  > list2[2])
     list3[2] = list[2];
     else
         list3[2] = list2[2];
     
     if(list[3]  > list2[3])
     list3[3] = list[3];
     else
         list3[3] = list2[3];
     
    if(list[4]  > list2[4])
     list3[4] = list[4];
     else
         list3[4] = list2[4];
     
     if(list[5]  > list2[5])
     list3[5] = list[5];
     else
         list3[5] = list2[5];
     
     if(list[6]  > list2[6])
     list3[6] = list[6];
     else
         list3[6] = list2[6];
     
     if(list[7]  > list2[7])
     list3[7] = list[7];
     else
         list3[7] = list2[7];
     
     
     if(list[8]  > list2[8])
     list3[8] = list[8];
     else
         list3[8] = list2[8];
     
     
     if(list[9]  > list2[9])
     list3[9] = list[9];
     else
         list3[9] = list2[9];
    */
     
    // again, this will work because list2 and list and list4 are same length
     
    for (int r =0; r < list.length; r++)
    {
    if (list[k] < list2[k])
    list4[k] = list[k];
     
    else
    list4[k] = list2[k];
    }
     
    /*
     if(list[0]  < list2[0])
     list4[0] = list[0];
     else
         list4[0] = list2[0];
     
     
     if(list[1]  < list2[1])
     list4[1] = list[1];
     else
         list4[1] = list2[1];
     
     if(list[2]  < list2[2])
     list4[2] = list[2];
     else
         list4[2] = list2[2];
     
     if(list[3]  < list2[3])
     list4[3] = list[3];
     else
         list4[3] = list2[3];
     
    if(list[4]  < list2[4])
     list4[4] = list[4];
     else
         list4[4] = list2[4];
     
     if(list[5]  < list2[5])
     list4[5] = list[5];
     else
         list4[5] = list2[5];
     
     if(list[6]  < list2[6])
     list4[6] = list[6];
     else
         list4[6] = list2[6];
     
     if(list[7]  < list2[7])
     list4[7] = list[7];
     else
         list4[7] = list2[7];
     
     
     if(list[8]  < list2[8])
     list4[8] = list[8];
     else
         list4[8] = list2[8];
     
     
     if(list[9]  < list2[9])
     list4[9] = list[9];
     else
         list4[9] = list2[9];
     
    */
     
     
    System.out.println();
     
    String[] str5 = new String[list3.length];
    String str6 = "";
    for (int w = 0; w < list3.length; w++)
    {
    if (w ==0)
    str5[w] = list3[w];
     
    else 
    str5[w] = str5[w-1] + " " + list3[w].toString();
     
    if (w == list3.length -1)
    str6= str5[w];
     
    }
     
            System.out.println("List 3 = " + str6);
     
    /*
    System.out.println("List 3 = "  + list3[0] + " " +list3[1] + " " + list3[2] + " "
             + list3[3]+" " + list3[4] + " " +list3[5] + " " + list3[6] + " " + list3[7]
            + " "+ list3[8] + " " + list3[9]);
    */
     
    String[] str7 = new String[list4.length];
    String str8 = "";
    for (int w = 0; w < list4.length; w++)
    {
    if (w ==0)
    str7[w] = list4[w];
     
    else 
    str7[w] = str7[w-1] + " " + list4[w].toString();
     
    if (w == list4.length -1)
    str8= str8[w];
     
    }
     
            System.out.println("List 4 = " + str8);
     
    /*
    System.out.println("List 4 = "  + list4[0] + " " +list4[1] + " " + list4[2] + " "
             + list4[3]+" " + list4[4] + " " +list4[5] + " " + list4[6] + " " + list4[7]
            + " "+ list4[8] + " " + list4[9]);
     
    */
     
     
     
        }
    }
    Now it'll look nicer.
    Last edited by javapenguin; November 11th, 2010 at 06:30 PM.

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    28
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: help with this, just a question

    its still kinda long though can im thinking of using a for loop for this