Re: Program will add but cannot get it to multiply
- From: "Monique Y. Mudama" <spam@xxxxxxxxxxxxxxxx>
- Date: Tue, 31 Jan 2006 13:50:17 -0700
On 2006-01-31, shannon penned:
> Hi,
> Just started Java and want to multiply odd numbers between 1 and 100.
> However, at the moment i can only get them to add. I tried replacing
> the + sign with the * multiplication sign but to no avail. Any
> suggestions?
>
>
> int sum = 0;
>
> for ( int number = 0; number <=100; number +=2 )
> sum += number;
>
>
> JOptionPane.showMessageDialog ( null, "The sum is " + sum, "Sum Odd
> Integerts from 1 to 100", JOptionPane.INFORMATION_MESSAGE );
Yup. Walk through the loop.
Your loop starts with number = 0.
Then you multiply sum by 0, resulting in sum = 0.
And then you keep multiplying 0 by stuff, all of which of course
results in 0.
--
monique
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
.
- References:
- Program will add but cannot get it to multiply
- From: shannon
- Program will add but cannot get it to multiply
- Prev by Date: Program will add but cannot get it to multiply
- Next by Date: Re: Program will add but cannot get it to multiply
- Previous by thread: Program will add but cannot get it to multiply
- Next by thread: Re: Program will add but cannot get it to multiply
- Index(es):
Relevant Pages
|