Results 1 to 5 of 5
Thread: Help with output
- 01-17-2013, 09:39 AM #1
Member
- Join Date
- Jan 2013
- Posts
- 2
- Rep Power
- 0
Help with output
Hi,
So I'm trying to figure out why I keep getting this output:
a-b c-d-
when I'm trying to get this output:
a-b c-d
It's driving me crazy. My print command is bounded within the loop function. In the end, x should equal zero before it hits the end of the loop. Please help!
Thanks!Java Code:class shuffle1 { public static void main (String[] args) { int x = 3; while (x > 0) { if (x > 2) { System.out.print ("a"); } if (x == 2) { System.out.print ("b c"); } if (x == 1) { System.out.print ("d"); x = x - 1; } x = x - 1; System.out.print ("-"); } System.out.println(); } }
C
- 01-17-2013, 10:01 AM #2
Member
- Join Date
- Nov 2012
- Location
- India
- Posts
- 70
- Rep Power
- 0
Re: Help with output
Hi,
you will change your code like below.... I think that will be works...
class shuffle1 {
public static void main (String[] args) {
int x = 3;
while (x > 0) {
if (x > 2) {
System.out.print ("a");
}
if (x == 2) {
System.out.print ("b c");
}
if (x == 1) {
System.out.print ("d");
x = x - 1;
}
x = x - 1;
if(x!=0){
System.out.print ("-");
}
}
System.out.println();
}
}
thanks,
- 01-17-2013, 10:11 AM #3
Member
- Join Date
- Jan 2013
- Posts
- 2
- Rep Power
- 0
Re: Help with output
I changed it like you posted. I'm still getting the same output:
a-b c-d-
It's strange. That little dash at the back is really annoying.
- 01-17-2013, 10:44 AM #4
Member
- Join Date
- Nov 2012
- Location
- India
- Posts
- 70
- Rep Power
- 0
Re: Help with output
Hi,
This is definitely works Already i was tested..
So use this....
public class shuffle1 {
public static void main (String[] args) {
int x = 3;
while (x > 0) {
if (x > 2) {
System.out.print ("a");
}
if (x == 2) {
System.out.print ("b c");
}
if (x == 1) {
System.out.print ("d");
}
x = x - 1;
if(x!=0){
System.out.print ("-");
}
}
System.out.println();
}
}
Thanks,
- 01-17-2013, 11:20 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
Re: Help with output
I find it a vey convoluted way of printing such a simple line; the simplest way to print it is:
but if you really, really want to use a loop here: three parts are printed: "a-" "b c" and "-d"; why not use it and do:Java Code:System.out.println("a-b c-d");
kind regards,Java Code:for (int i=0; i < 3; i++) { switch (i) { case 0: System.out.print("a-"); break; case 1: System.out.print("b c"); break; case 2: System.out.println("-d"); break; } }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
XSLT | disable-output-escaping adding a new line to the output
By smarty_m2002 in forum Advanced JavaReplies: 2Last Post: 05-03-2012, 11:39 AM -
Can't See the output
By Gousia in forum New To JavaReplies: 12Last Post: 02-28-2011, 11:01 AM -
how to get the resultset output into an output file
By renu in forum New To JavaReplies: 0Last Post: 09-30-2010, 08:16 PM -
cannot see the output...
By Mihail Kravsun in forum New To JavaReplies: 3Last Post: 04-15-2009, 06:57 AM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks