Hi, Can anyone go through my code to brush it up?I got the correct output,printed below, but i need some polishing of the below loop assignment.
1.Should i be using 2 while loops?
2.Regarding the output-How can i align the verticle columns and the output neatly.

public class MK{
	public static void main(String[] args){
		double ks;
		double ml;
		
		System.out.printf("\n%s%20s%20s%20s\n","Miles(ms)","Kilometers(ks)","Kilometers(km)","Miles(ml)");
		
		double km = 20,ms = 1;
		while( ms <= 10 ) {
		while( km <= 65 ) {
			ks =( 1.609*ms );
			ml =( km/1.609 );
			System.out.printf( "%1.0f%19.3f%15.0f%30.3f\n",ms,ks,km,ml );
			ms +=1;
			km +=5;
		}
		}
			
			
		
	}
}

OUTPUT

Miles(ms) Kilometers(ks) Kilometers(km)      Miles(ml)
1              1.609             20                 12.430
2              3.218             25                 15.538
3              4.827             30                 18.645
4              6.436             35                 21.753
5              8.045             40                 24.860
6              9.654             45                 27.968
7             11.263             50                 31.075
8             12.872             55                 34.183
9             14.481             60                 37.290
10             16.090             65                 40.398

Recommended Answers

All 4 Replies

1. If there are two loops just because there are two termination conditions then no, one is enough if you AND the two conditions together:
while (condition1 && condition2) { ...
2. printf formats have an optional width spec that sets the column width (by left-adding with blanks). Documentation is in the usual places.

1. If there are two loops just because there are two termination conditions then no, one is enough if you AND the two conditions together:
while (condition1 && condition2) { ...
2. printf formats have an optional width spec that sets the column width (by left-adding with blanks). Documentation is in the usual places.

i dont understand ur answer 2.

Check out the format specs for printf. You can format the Miles column so its always the same width, so all the subsequent columns line up properly.

Yep...if i knew i wont hav seeked help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.