Outline ·
[ Standard ] ·
Linear+
java looping
TSH3LLKEEPER
|
Mar 5 2015, 10:01 PM, updated 10y ago
|
|
Here is my code » Click to show Spoiler - click again to hide... « CODE public static void main (String args []){ System.out.println ("Miles"+"\t"+"Kilometers|"+"\t"+"Kilometers"+"\t"+"Miles"); int miles; int km; for (km=20;km<=65;km+=5) { for (miles=1;miles<=10;miles++) { System.out.println (miles+"\t"+(miles*1.609)+"\t"+"|"+"\t"+km+"\t\t"+(km*0.621)); } } } }
The answer came out Miles Kilometers| Kilometers Miles 1 1.609 | 20 12.42 2 3.218 | 20 12.42 3 4.827 | 20 12.42 Where i need it to be like this Miles Kilometers| Kilometers Miles 1 1.609 | 20 12.42 2 3.218 | 25 15.53 Is there any code that i missed out or is there an error in my code any help is much appreciated
|
|
|
|
dewill
|
Mar 5 2015, 10:12 PM
|
|
since your code using double for. 2nd iteration for, need to generate till end only go to next for from 1st iteration
a1 b1 a1 b2 a2 b1 a2 b2 a3 b1 a3 b2
|
|
|
|
TSH3LLKEEPER
|
Mar 5 2015, 10:20 PM
|
|
QUOTE(dewill @ Mar 5 2015, 09:12 PM) since your code using double for. 2nd iteration for, need to generate till end only go to next for from 1st iteration a1 b1 a1 b2 a2 b1 a2 b2 a3 b1 a3 b2 understood the point, should i use another code?
|
|
|
|
dewill
|
Mar 6 2015, 12:17 AM
|
|
QUOTE(H3LLKEEPER @ Mar 5 2015, 10:20 PM) understood the point, should i use another code? dismiss 1 for and update your equation
|
|
|
|
wjwong696
|
Mar 16 2015, 08:25 PM
|
New Member
|
QUOTE(dewill @ Mar 6 2015, 12:17 AM) dismiss 1 for and update your equation Remove the outer loop. Increament the variable from outer loop inside the loop lefted.
|
|
|
|
wjwong696
|
Mar 16 2015, 08:35 PM
|
New Member
|
QUOTE(H3LLKEEPER @ Mar 5 2015, 10:01 PM) Here is my code » Click to show Spoiler - click again to hide... « CODE public static void main (String args []){ System.out.println ("Miles"+"\t"+"Kilometers|"+"\t"+"Kilometers"+"\t"+"Miles"); int miles; int km; for (km=20;km<=65;km+=5) { for (miles=1;miles<=10;miles++) { System.out.println (miles+"\t"+(miles*1.609)+"\t"+"|"+"\t"+km+"\t\t"+(km*0.621)); } } } }
The answer came out Miles Kilometers| Kilometers Miles 1 1.609 | 20 12.42 2 3.218 | 20 12.42 3 4.827 | 20 12.42 Where i need it to be like this Miles Kilometers| Kilometers Miles 1 1.609 | 20 12.42 2 3.218 | 25 15.53 Is there any code that i missed out or is there an error in my code any help is much appreciated Short explanation: combine both loop.
|
|
|
|
bumpo
|
Mar 18 2015, 03:38 PM
|
|
are you trying to achieve just 10 loop instead of your current 100?
if so...
for (int i=1; i <= 10; i++) { System.out.println (i+"\t"+(i*1.609)+"\t"+"|"+"\t"+(15 +(i*5))+"\t\t"+((15 +(i*5))*0.621)); }
1 1.609 | 20 12.42 2 3.218 | 25 15.525 3 4.827 | 30 18.63 4 6.436 | 35 21.735 5 8.045 | 40 24.84 6 9.654 | 45 27.945 7 11.263 | 50 31.05 8 12.872 | 55 34.155 9 14.481 | 60 37.26 10 16.09 | 65 40.365
|
|
|
|
TSH3LLKEEPER
|
Mar 19 2015, 11:07 AM
|
|
QUOTE(wjwong696 @ Mar 16 2015, 07:35 PM) Short explanation: combine both loop. QUOTE(bumpo @ Mar 18 2015, 02:38 PM) are you trying to achieve just 10 loop instead of your current 100? if so... for (int i=1; i <= 10; i++) { System.out.println (i+"\t"+(i*1.609)+"\t"+"|"+"\t"+(15 +(i*5))+"\t\t"+((15 +(i*5))*0.621)); } 1 1.609 | 20 12.42 2 3.218 | 25 15.525 3 4.827 | 30 18.63 4 6.436 | 35 21.735 5 8.045 | 40 24.84 6 9.654 | 45 27.945 7 11.263 | 50 31.05 8 12.872 | 55 34.155 9 14.481 | 60 37.26 10 16.09 | 65 40.365 already solved it, but will take you guys's advice into research and see how to do it another way much appreciated for helping
|
|
|
|