Welcome Guest ( Log In | Register )

Bump Topic Topic Closed RSS Feed

Outline · [ Standard ] · Linear+

 java looping

views
     
TSH3LLKEEPER
post Mar 5 2015, 10:01 PM, updated 10y ago

Winning Is Everything!
*******
Senior Member
4,531 posts

Joined: Apr 2009


Here is my code
» Click to show Spoiler - click again to hide... «


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
notworthy.gif notworthy.gif


dewill
post Mar 5 2015, 10:12 PM

Regular
******
Senior Member
1,716 posts

Joined: Feb 2006


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
post Mar 5 2015, 10:20 PM

Winning Is Everything!
*******
Senior Member
4,531 posts

Joined: Apr 2009


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
post Mar 6 2015, 12:17 AM

Regular
******
Senior Member
1,716 posts

Joined: Feb 2006


QUOTE(H3LLKEEPER @ Mar 5 2015, 10:20 PM)
understood the point, should i use another code?
*
dismiss 1 for and update your equation
wjwong696
post Mar 16 2015, 08:25 PM

New Member
*
Junior Member
20 posts

Joined: Feb 2015
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
post Mar 16 2015, 08:35 PM

New Member
*
Junior Member
20 posts

Joined: Feb 2015
QUOTE(H3LLKEEPER @ Mar 5 2015, 10:01 PM)
Here is my code
» Click to show Spoiler - click again to hide... «


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
notworthy.gif  notworthy.gif
*
Short explanation: combine both loop.

bumpo
post Mar 18 2015, 03:38 PM

On my way
****
Junior Member
632 posts

Joined: Mar 2013


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
post Mar 19 2015, 11:07 AM

Winning Is Everything!
*******
Senior Member
4,531 posts

Joined: Apr 2009


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 thumbup.gif thumbup.gif
much appreciated for helping smile.gif

Topic ClosedOptions
 

Change to:
| Lo-Fi Version
0.0131sec    0.36    5 queries    GZIP Disabled
Time is now: 28th March 2024 - 11:56 PM