using a for loop to print out double sums

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

using a for loop to print out double sums

Post 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 :banghead:

help please devnet, tag ur in :?
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: using a for loop to print out double sums

Post by andyhoneycutt »

have you tried using a float instead of an integer for your loop?
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Re: using a for loop to print out double sums

Post 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
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: using a for loop to print out double sums

Post 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 )
...
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Re: using a for loop to print out double sums

Post 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?
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Re: using a for loop to print out double sums

Post 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?
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Re: using a for loop to print out double sums

Post by Obadiah »

didnt want to bump this but its seriously kickin my butt....im open for any suggestions....anyone?
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: using a for loop to print out double sums

Post 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
Post Reply