1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Anyone Wanna Help Me With Java?

Discussion in 'General Programming Chat' started by yamahafzr, Oct 19, 2009.

  1. yamahafzr

    yamahafzr Regular Member Premium Member

    Joined:
    Mar 29, 2009
    Messages:
    244
    Likes Received:
    154
    Location:
    NJ
    Hey, I am doing basic java and we are doing some pyramids and I am really struggling. I really want to learn java, so if anyone would be nice enough to help me out please let me know. I would think it would be easier to do it over aim rather then do it over the thread, but basically i have to make 4 pyramids
    *
    **
    ***
    ****
    *****

    *****
    ****
    ***
    **
    *

    -----------------*
    ---------------*-- *
    -------------* --* --*
    -----------* --* --* --*
    ---------*-- *-- *-- *-- *
    edit: fuck the above one took out the spaces so im gunna replace the spaces wit -

    and the above one flipped. I got the first 2, but i can't get the second 2. Also, they have to rely on the input, so for the above pyramids the input was 5. The input is between 1-20. Any help is greatly appreciated

    edit: wat i have for the top 2. this is our level of coding
    Code:
    package loops;
    import java.io.*;
    public class Main {  
        public static void main(String[] args) {   
    int userinput = 0;
    do
    {
        System.out.println("enter number less than 18");
        userinput = Cherokee.readInt();
    }while(userinput>= 18 && userinput<1);
    
    for(int x = 0;x<=userinput;)
    {
        for (int y = 0;y<x;y++)
            System.out.print("*");
               System.out.println("");
               x++;
              }
    System.out.println(" ");
    for(int x = 0;x<userinput;)
    {
    
        for (int y = 0;y<userinput;)
        {
    
           if(x>y)
               System.out.print(" ");
           else
               System.out.print("*");
         y++;
        }
    System.out.println("");
    x++;
    }
    System.out.println(" ");       
        } 
    }
    the cherokee. is from a class file that he made and we put it in the bottom of our code






    final edit: we can only use 2 loops per pyramid
     
    Last edited: Oct 19, 2009
  2. yamahafzr

    yamahafzr Regular Member Premium Member

    Joined:
    Mar 29, 2009
    Messages:
    244
    Likes Received:
    154
    Location:
    NJ
    bump cause i need this asap!
     
  3. BozoClown

    BozoClown Junior Member

    Joined:
    Jan 4, 2009
    Messages:
    150
    Likes Received:
    106
    It does look like you have a java problem. It is an algorithm issue. Write down your algorithm in pseudo-code then translate it to the programming language. You have to break down the problem in a clear manner.

    For example for your second triangle.
    1. Before you even start printing out the triangle you have to make sure that the base will fit (i.e. it won't be cut off).
    2. Write down the parameters that will be used in the calculation. e.g. asterisks, spaces (left spaces and spaces between the asterisks)
    3. Levels/height whatever you choose to call it. (i.e. the one you have drawn has 5 levels)

    Something like this

    Code:
    for each level i {
       print left spaces;
       print i asterisks with spaces in between;
    }
    
    The left spaces are determined by a formula that makes sure the triangle doesn't cut off (1 above) e.g. K_i = K_n + (n - i) where k_i are the spaces at level i, k_n are the left spaces at the last level n (n=5 for your diagram). You may need to a add a constant
    e.g. K_i = K_n + 2*(n - i).

    Hopefully that gives you a clear picture on how to tackle the problem.
     
    • Thanks Thanks x 1
    Last edited: Oct 19, 2009
  4. yamahafzr

    yamahafzr Regular Member Premium Member

    Joined:
    Mar 29, 2009
    Messages:
    244
    Likes Received:
    154
    Location:
    NJ
    im still pretty confused, and thats prolly cause ur using K i and n, but i can prolly decipher that. thanks for your help. u just gave me the idea to write everything down xD
     
  5. BozoClown

    BozoClown Junior Member

    Joined:
    Jan 4, 2009
    Messages:
    150
    Likes Received:
    106
    I haven't worked with java in a long while but this loop should be close the the pseudo code. I have based it on your code.

    Code:
    for(int i=1; i<=userinput; i++){
      int spaces = 0;
      int left_spaces = 6 + (userinput - i);
      while(spaces < left_spaces){
        System.out.print(" ");
        spaces++;
      }
      
      int asterisks_printed = 0;
      while(asterisks_printed <= i){
        System.out.print("* ");
        asterisks_printed++;
      }
      
      System.out.println("");
    }
    
     
    • Thanks Thanks x 1
  6. yamahafzr

    yamahafzr Regular Member Premium Member

    Joined:
    Mar 29, 2009
    Messages:
    244
    Likes Received:
    154
    Location:
    NJ
    damn bro thank you so much. im def gunna run through and change the variables and stuff not only for the sake of matching it to the rest, but so i can acctually learn some java. much respect. hit me up if u ever need anything. it is missing the top * but im sure i can figure that out
     
  7. yamahafzr

    yamahafzr Regular Member Premium Member

    Joined:
    Mar 29, 2009
    Messages:
    244
    Likes Received:
    154
    Location:
    NJ
    fixed the problem in ur code and did the other triangle. thanks again bro =)