Results 1 to 12 of 12
  1. #1
    earthkid123 is offline Member
    Join Date
    Mar 2014
    Posts
    16
    Rep Power
    0

    Default Getting rid of astericks

    This is my code so far which is correct my only problem is that I have to make it so there doesn't become more than 50 astericks on a line and I don't know how to do that I know the code is something divided by 50 but I dont understand where I would put it.
    so instead of this
    ************************************************** ******************(pretend it was 100 astericks)
    it would be
    ****************************************(x<50 astericks or 42)
    import java.util.*;
    public class Graph {
    private int [] counter;

    public Graph(int x)
    {
    counter=new int[5];
    }
    public void countIt(int x)
    {
    counter[x]++;
    }

    public void GraphIt()
    {
    for(int i =0; i<=1000000; i++)
    {

    for(int j=0; j<counter[i]-1;j++)
    {
    System.out.print("*");
    }

    System.out.println("");

    }
    }
    }
    class TestMyGraph
    {
    public static void main(String[] args)
    {
    Graph g= new Graph(5);
    Random r=new Random();
    g.countIt(r.nextInt(5));
    for(int k=0; k<=1000;k++)
    {
    g.countIt(r.nextInt(5));

    }
    g.GraphIt();
    }
    }

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Getting rid of astericks

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    there doesn't become more than 50 astericks on a line
    Count the asterisks as they are printed on a line and stop printing them when the count gets to 50.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    earthkid123 is offline Member
    Join Date
    Mar 2014
    Posts
    16
    Rep Power
    0

    Default Re: Getting rid of astericks

    Java Code:
    import java.util.*;
     public class Graph {
     private int [] counter;
    
     public Graph(int x)
     {
         counter=new int[5];
     }
     public void countIt(int x)
     {
       counter[x]++; 
     }
     
     public void GraphIt()
       {
          for(int i =0; i<=1000000; i++)
          {
             
             for(int j=0; j<counter[i]-1;j++)
             { 
               System.out.print("*"); 
             }
           
               System.out.println("");
        
          }
       }
     }
    class TestMyGraph
    {
     public static void main(String[] args) 
      {   
         Graph g= new Graph(5);
         Random r=new Random();
         g.countIt(r.nextInt(5));
         for(int k=0; k<=1000;k++)
         {
             g.countIt(r.nextInt(5));
             
         }
         g.GraphIt();
      }
    }
    would I use an if statement in order to do that

  4. #4
    earthkid123 is offline Member
    Join Date
    Mar 2014
    Posts
    16
    Rep Power
    0

    Default Re: Getting rid of astericks

    It needs to still be in this format I just need it to divide at 50 each time it goes over 50

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Getting rid of astericks

    Yes, an if statement could be used to detect a value of the counter.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    earthkid123 is offline Member
    Join Date
    Mar 2014
    Posts
    16
    Rep Power
    0

    Default Re: Getting rid of astericks

    okay so:

    if(counter[i]>50)
    counter[i]/50;
    ? would it be like that and where would I put it inbtween the nested for loop

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Getting rid of astericks

    Try one then the other to see what they do.

    What is the code supposed to do when 50 is reached? What code should be inside of the if?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    earthkid123 is offline Member
    Join Date
    Mar 2014
    Posts
    16
    Rep Power
    0

    Default Re: Getting rid of astericks

    public void GraphIt()
    {
    for(int i =0; i<=1000000; i++)
    {

    for(int j=0; j<counter[i]-1;j++)
    {
    System.out.print("*");

    if(counter[j]>50)
    {
    counter[i]=counter[j]/50;
    }
    }
    System.out.println("");

    }
    }
    }
    THis is it I believe

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Getting rid of astericks

    THis is it I believe
    Please explain what "it" is.
    Have you solved your problem now?

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    earthkid123 is offline Member
    Join Date
    Mar 2014
    Posts
    16
    Rep Power
    0

    Default Re: Getting rid of astericks

    Java Code:
    public void GraphIt()
    {
    for(int i =0; i<=1000000; i++)
    {
    
    for(int j=0; j<counter[i]-1;j++)
    { 
    System.out.print("*"); 
    
    if(counter[j]>50)
    {
    counter[i]=counter[j]/50;
    }
    }
    System.out.println("");
    
    }
    }
    }
    Now what the if statement is going to do is the print statement every time it goes over 50 astericks based off of the random number then it will divide by 50 and start over again and repeat the process so it keeps track the array from 0-4 then will divide by 50 every time it becomes more than 50

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default Re: Getting rid of astericks

    Does that code do what you want?

    The formatting is messed up in the last post. All the indentations have been lost.

    Hint for testing: Don't use such large numbers. Use a number that is easily verified by looking at the printout. Say 5 vs 50 and 100 vs 1000
    Last edited by Norm; 03-19-2014 at 09:22 PM.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    earthkid123 is offline Member
    Join Date
    Mar 2014
    Posts
    16
    Rep Power
    0

    Default Re: Getting rid of astericks

    Yes it does percisley what I want I understand its messed up because of how I copied it in it is nicely formatted in my IDE thank you for your help

Similar Threads

  1. Replies: 8
    Last Post: 05-19-2011, 09:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •