another java question  | |
March 8th, 2002, 01:54 PM
|
#1 (permalink)
| | Member
Join Date: Oct 2001 Location: St. Louis, MO
Posts: 463
|
Okay here's another puzzler for you. I have program that needs to display the numbers 1 -5 and then the same numbers times 10, 100, & 1000.
package ex416;
import javax.swing.JOptionPane;
public class ex416 {
public static void main( String args[] )
{
String output;
int number;
number = 1;
output = "";
while ( number < 6 ) {
output = "N 10*N 100*N 1000*N\n" +
(number) + " " + ( number * 10 ) + " " + ( number * 100) +
" " + ( number * 1000);
number = number + 1;
}
JOptionPane.showMessageDialog( null, output,
"Results of Program",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}
This displays the last number (5) only though, and not 1 - 4. There's something fishy with the loop or the results display. I appreciate any help. Thanks,
Jeff |
| |
March 8th, 2002, 01:56 PM
|
#2 (permalink)
| | Member
Join Date: Oct 2001 Location: St. Louis, MO
Posts: 463
|
If I change the code to this:
package ex416;
import javax.swing.JOptionPane;
public class ex416 {
public static void main( String args[] )
{
String output;
int number;
number = 1;
output = "";
while ( number < 6 ) {
output = "N 10*N 100*N 1000*N\n" +
(number) + " " + ( number * 10 ) + " " + ( number * 100) +
" " + ( number * 1000);
JOptionPane.showMessageDialog( null, output,
"Results of Program",
JOptionPane.INFORMATION_MESSAGE );
number = number + 1;
}
System.exit( 0 );
}
}
I get the proper results in 5 JOptionPane windows. Now I need to get it all in one window.
Jeff |
| |
March 8th, 2002, 02:14 PM
|
#3 (permalink)
| | Member
Join Date: Oct 2001 Location: St. Louis, MO
Posts: 463
|
I changed the JOptionPane to system.out.println and received the results that I wanted. Thanks,
Jeff |
| |
March 8th, 2002, 02:30 PM
|
#4 (permalink)
| | Senior Member
Join Date: Oct 2001 Location: Utah
Posts: 551
|
hey bud,
Looks like good code, but let me help you out a bit.
It would really help you understand, to walk through the program just like the computer will. Meaning, step through it, and on paper write down the variables at each step.
So, you're first code was good, except output variable got overwritten each pass of the loop, and only had the last thing in it when it output to the joption pane.
while ( number < 6 ) { output= output + "\n";
output = output = output + "N 10*N 100*N 1000*N\n" +
(number) + " " + ( number * 10 ) + " " + ( number * 100) +
" " + ( number * 1000);
number = number + 1;
}
JOptionPane.showMessageDialog( null, output,
"Results of Program",
JOptionPane.INFORMATION_MESSAGE );
but, add in what I put in read, and then it just adds on each new group of numbers instead of overwriting.
Your second try opened up a new option pane for every time through the loop, where as your final working thing just printed it to system.out, each time through the loop.
good luck, and keep at it.
dragonb
Last edited by dragonb : March 8th, 2002 at 02:33 PM.
|
| |
March 8th, 2002, 06:58 PM
|
#5 (permalink)
| | Banned
Join Date: Oct 2001
Posts: 447
|
dragonb, Quote: |
It would really help you understand, to walk through the program just like the computer will. Meaning, step through it, and on paper write down the variables at each step.
| Excellent advice. The ability to debug and develop incrementally (take baby steps) is fundamental to programming well, imho. All too often people write the whole program, then wonder why it doesn't work...
Now as far as "good code", humm: Code: String output ="";
String temp ="";
for (int i = 1; i <6;i++)
{
//format as needed
temp = i + ", " + i*10 + ", " + i*100 + ", " + i*1000 + "\n";
output =+ temp;
}
//do whatever you want with output Now, one can argue that the while and for loops are logically the same thing, but you know you're doing 1-5, so just do that (gotta luv for loops).
Also, the first time running the proggy, slap some system.out.println's for each loop (output temp) and end result (output output), thus verifying you get what you want, then remove/comment those lines. |
| | | Thread Tools | Search this Thread | | | | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Most Active Discussions | | | | | Recent Discussions  | | | | | |