Results 1 to 12 of 12
Thread: Getting rid of astericks
- 03-19-2014, 09:20 PM #1
Member
- Join Date
- Mar 2014
- Posts
- 16
- Rep Power
- 0
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();
}
}
- 03-19-2014, 09:26 PM #2
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 lineIf you don't understand my response, don't ignore it, ask a question.
- 03-19-2014, 09:34 PM #3
Member
- Join Date
- Mar 2014
- Posts
- 16
- Rep Power
- 0
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(); } }
- 03-19-2014, 09:40 PM #4
Member
- Join Date
- Mar 2014
- Posts
- 16
- Rep Power
- 0
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
- 03-19-2014, 09:42 PM #5
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.
- 03-19-2014, 09:45 PM #6
Member
- Join Date
- Mar 2014
- Posts
- 16
- Rep Power
- 0
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
- 03-19-2014, 09:58 PM #7
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.
- 03-19-2014, 10:07 PM #8
Member
- Join Date
- Mar 2014
- Posts
- 16
- Rep Power
- 0
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
- 03-19-2014, 10:11 PM #9
Re: Getting rid of astericks
THis is it I believe
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.
- 03-19-2014, 10:13 PM #10
Member
- Join Date
- Mar 2014
- Posts
- 16
- Rep Power
- 0
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(""); } } }
- 03-19-2014, 10:18 PM #11
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 1000Last edited by Norm; 03-19-2014 at 10:22 PM.
If you don't understand my response, don't ignore it, ask a question.
- 03-19-2014, 10:22 PM #12
Member
- Join Date
- Mar 2014
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
paintComponent(page), Reverse digit, and Astericks triangle outputter question
By silverglade in forum New To JavaReplies: 8Last Post: 05-19-2011, 10:56 PM
Bookmarks