Results 1 to 20 of 21
Thread: How to print this in java?
- 05-13-2010, 01:04 PM #1
Member
- Join Date
- May 2010
- Posts
- 1
- Rep Power
- 0
- 05-13-2010, 02:56 PM #2
1. This seems like entry level homework (since I did this)
2. Wrong section.
3. Research Loops
4. This example should be in google
5. System.out.println:rolleyes: ~ Sno ~ :rolleyes:
'-~ B.S. Computer Science ~-'
-
- 05-13-2010, 11:34 PM #4
One answer is to Use the println() statement. Code the data for the each line followed by the newline characters until you've got all the lines
Or is your question, how to generate the data?
Look at the data, line by line.
Two things can be seen:
The data gets longer for each line
The order of the data reverses line by line
Make a method that takes the line number and returns the data as needed for that line.
- 05-14-2010, 08:25 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 05-14-2010, 12:23 PM #6
Member
- Join Date
- Apr 2010
- Posts
- 55
- Rep Power
- 0
Just post if you need an explanationJava Code:boolean reverse = false; for(int i=0; i<6;i++) { if(reverse) { for(int j=1, j<i; j++) { System.out.print(j); reverse=!reverse; } } else { for(int j=i; j>0; j--) { System.out.print(j); reverse=!reverse; } } System.out.println();//j was a typo sorry }Last edited by bayan; 05-16-2010 at 11:01 AM.
- 05-14-2010, 02:04 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
You could always explain...why?
- 05-14-2010, 02:28 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 05-14-2010, 02:36 PM #9
- 05-14-2010, 02:50 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 05-14-2010, 02:50 PM #11
Member
- Join Date
- Nov 2007
- Posts
- 2
- Rep Power
- 0
In groovy, it would look like this:
(1..6).each{
((it%2) ? (it..1) : (1..it)).each{print "$it ";}}
println ""
- 05-14-2010, 02:53 PM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 05-14-2010, 02:56 PM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
- 05-14-2010, 04:33 PM #14
Member
- Join Date
- Nov 2007
- Posts
- 2
- Rep Power
- 0
Points about Groovy Taken. However, there are 2 things to notice from the Groovy approach that ported, in various degrees of complexity to Java.
1) reverse = !reverse can be accomplished with alternative means. if(i % 2 == 1) <do reverse> else <do forward>. That looks a bit ugly, but processing of "rows" based on the remainder function is a pretty common solution to this type of problem.
2) Closures have been discussed in regards to Java for quite a while. The "how you iterate" vs. "what you do in the loop" is. In the java world, those two concepts are tightly coupled.
Taking #1 into account, here's what the algorithm looks like:
It's important to note that #2 shouldn't be addressed in a Java world on a simple case like this one, but should be thought about for more complex scenarios.for(int i=1; i<=6;i++)
{
if(i%2==1)}
{
for(int j=1; j<=i; j++)}
System.out.print(String.valueOf(j)+" " );
else
{
for(int j=i; j>0; j--)}
System.out.print(String.valueOf(j)+" " );
System.out.println();
- 05-15-2010, 08:42 AM #15
Member
- Join Date
- May 2010
- Location
- china
- Posts
- 7
- Rep Power
- 0
I can frome china,I want to make friend with you,My MSN is esxwazq123@hotmail.com
My replay:
int target = 6;
for(int i = 1;i<=target;i++){
if(i%2!=0){
for(int j = 1; j<=i; j++)
System.out.print(j);
}else{
for(int k= i; k>0; k--)
System.out.print(k);
}
System.out.println();
}
- 05-28-2010, 06:42 AM #16
Member
- Join Date
- Mar 2010
- Posts
- 9
- Rep Power
- 0
here s it.
A little more interactive. You can enter (at runtime) the number of lines for which you want to repeat this pattern.
Java Code:import java.io.*; public class Pattern{ public static void main (String[] args) throws IOException{ System.out.print("Enter the number of lines for which you want this pattern.: \t"); int noOfLines = Integer.parseInt((new BufferedReader(new InputStreamReader(System.in))).readLine()); for(int i=1; i<=noOfLines;i++) { if((i%2) == 0) { for(int j=i; j>=1; j--) { System.out.print(j + " "); } } else { for(int j=1; j<=i; j++) { System.out.print(j + " "); } } System.out.println(); } } }Last edited by Eranga; 05-28-2010 at 02:00 PM. Reason: code tags added
- 05-28-2010, 02:01 PM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Please use code tags when you are posting here in the forum. Unformated codes are really hard to read. If you don't know how to do it, check on my forum signature.
- 05-28-2010, 02:10 PM #18
Member
- Join Date
- Mar 2010
- Posts
- 9
- Rep Power
- 0
Point taken. !!
Java Code:import java.io.*; public class Pattern{ public static void main (String[] args) throws IOException{ System.out.print("Enter the number of lines for which you want this pattern.: \t"); int noOfLines = Integer.parseInt((new BufferedReader(new InputStreamReader(System.in))).readLine()); for(int i=1; i<=noOfLines;i++) { if((i%2) == 0) { for(int j=i; j>=1; j--) { System.out.print(j + " "); } } else { for(int j=1; j<=i; j++) { System.out.print(j + " "); } } System.out.println(); } } }
- 05-28-2010, 02:12 PM #19
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
No need of doing that actually. I've already do that, keep in mind to do it next time onwards. :)
- 05-28-2010, 02:15 PM #20
Member
- Join Date
- Mar 2010
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
How to use and print the JAVA code from a JSP
By Ginkan in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 02-23-2010, 02:05 PM -
Print .prn file using java
By nishant in forum Advanced JavaReplies: 0Last Post: 07-14-2009, 09:15 AM -
Print the text file and print preview them
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM -
print in java linux
By bbq in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 04:24 AM -
print .doc in java
By Alan in forum Advanced JavaReplies: 1Last Post: 05-17-2007, 04:05 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks