do
{
Value = d.input.readInt();
if (Value <=0 || Value >=101) Value = 0;
else
Sum = Sum + Value;
}while (Value != (-9999));
Sum = Sum + (9999);
My program lets the user input values and then adds them to (Sum). But, valid values can only be between 1 and 100 inclusive, and entering -9999 terminates the value entering stage, so the result of Sum can be output. This only part of the code, but whenever i try to run it (compiles fine) it just hangs after i enter -9999 (not freeze, it's as if it's waiting for more values). I'm pretty sure it's cos it's seeing -9999 as an invalid value, but i don't know how to get round this.
Thankies in advance.
(Note- I'm using an extra package called "Element", as this is required by the course i'm on: http://www.cs.williams.edu/~bailey/JavaElements/")
Do your own homework, dude.
(Also, try actually reading through your program step by step and seeing what it actually does.)
while(cond){blah}
> do{blah}while(cond)
>>3
unless you're making a stylistic point (which is certainly valid), there's nothing wrong with post-test loop in this situation.
if(Value==-9999) break;
use recursion.
int Add(int Value){
Value+=Value;
if(Value <=0 || Value >=101) return Value;
else return Add(Value);
}
void main(){
int Sum=0; //u need to set it default....read ur lines..
Sum=Add(Sum);
}
//there we go. When the input is not between 100 - 0. program will quit. But when it is between 100 - 0; it will add and return Sum.
and yes i agree with other ppl. DO YOUR GOD DAMN HOME WORK SOMEWHERE ELSE....PLEASE.
I note that your language has too many parenthesis. May I suggest a language with fewer?
(loop for value = 0 then (read)
until (= value -9999)
summing value)
>>7
Owned in parenthesis count by parenthesiscode. Ow.
Your thread title was also kind of... shitty.
jaja use perl kekeke
int sum = 0;
int value;
while ((value = d.input.readInt()) != -9999) {
if (value >0 && value <= 100) {
sum += value;
}
}
int Add(int Value){
Value+=Value;
if(Value <=0 && Value >=101) return Value;
else return Add(Value);
}
void main(){
int Sum=0;
Sum=Add(Sum);
}
(loop for value = 0 then (read)
until (= value -9999)
when (<= 1 value 100) sum value)
>>13 Parenthesis, gross!!
$_ > 0 and $_ <= 100 and $sum += $_ and undef $_ while $_ = <STDIN> and $_ != -9999
>>14 you still need to print $sum
at the end.
True. Add
or print $sum and exit
at the end
"and exit" is redundant, no?
but it's very poetic