Page 1 of 1
using a for loop to print out double sums
Posted: Tue Oct 21, 2008 1:36 pm
by Obadiah
so im trying a go at java again and in school we never had to do this but surfing online i saw that its possible with a for each but not specifically the way i need it to work
heres what i think i know...emphasis on the think;
i cant just do this however i wish i could
Code: Select all
for(int i = 7.56; i < 56; i += 1.02){
System.out.println(i);}
there has got to be an array from what the internet tells me however no good examples in my book
help please devnet, tag ur in

Re: using a for loop to print out double sums
Posted: Tue Oct 21, 2008 3:59 pm
by andyhoneycutt
have you tried using a float instead of an integer for your loop?
Re: using a for loop to print out double sums
Posted: Tue Oct 21, 2008 5:00 pm
by Obadiah
just tried using a float however i think i used it the wrong way
i got as a build error....
C:\Documents and Settings\administrator\My Documents\JCreator LE\MyProjects\stocks.java:12: possible loss of precision
found : double
required: float
for(float i = 7.56; i < 56; i += 1.02){
}
funny enough it processed the code and gave me an output of
List average is 25.4
*edited
using double throws a very bad error....not sure why
Re: using a for loop to print out double sums
Posted: Wed Oct 22, 2008 2:00 pm
by andyhoneycutt
Code: Select all
for(float i = 7.56; i < 56; i += 1.02){
}
Try:
Code: Select all
for( float i = 7.56[color=#FF0000]f[/color]; i < 56; i += 1.02 )
...
Re: using a for loop to print out double sums
Posted: Thu Oct 23, 2008 2:30 am
by Obadiah
andyhoneycutt wrote:Code: Select all
for(float i = 7.56; i < 56; i += 1.02){
}
Try:
Code: Select all
for( float i = 7.56[color=#FF0000]f[/color]; i < 56; i += 1.02 )
...
i tried another method....i got results but its not working as it should
it adds the 1.02 to the 7.56 but it just does it one time, and it loops indefinately instead of 56 times and i cant figure out where i fouled it up
Code: Select all
class example {
public static void main(String[]args) {
double x,y;
for(x = 1; x < 56; x++){
x=7.56;
y = x + 1.02;
System.out.println(x);
}
}
}
well...at least i got progress this time....however howw do i get it to stop looping indefinately and why wont it keep adding the 1.02?
Re: using a for loop to print out double sums
Posted: Thu Oct 23, 2008 2:56 am
by Obadiah
ok...i got the first part of my last two questions....because i set xto 56 and then tried to set x again to 7.56 and giving y its value....made y make the for loop run indefinately...now what?
Re: using a for loop to print out double sums
Posted: Sat Oct 25, 2008 10:37 pm
by Obadiah
didnt want to bump this but its seriously kickin my butt....im open for any suggestions....anyone?
Re: using a for loop to print out double sums
Posted: Wed Nov 19, 2008 10:58 am
by andyhoneycutt
Obadiah wrote:i tried another method....i got results but its not working as it should
it adds the 1.02 to the 7.56 but it just does it one time, and it loops indefinately instead of 56 times and i cant figure out where i fouled it up
Code: Select all
class example {
public static void main(String[] args) {
[color=#BF0000]float[/color] x,y;
for([color=#BF0000]x = 1.00[/color]; x < [color=#BF0000]56.00[/color]; [color=#BF0000]x += 1.02[/color]){
System.out.println(x);
}
}
}
This does not work? Please post your new code
-Andy