Halp plox - Java (21)

1 Name: #!/usr/bin/anonymous : 2008-01-18 13:48 ID:Pxk0ukNO

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/")

2 Name: #!/usr/bin/anonymous : 2008-01-18 14:29 ID:Heaven

Do your own homework, dude.

(Also, try actually reading through your program step by step and seeing what it actually does.)

3 Name: #!/usr/bin/anonymous : 2008-01-18 22:20 ID:Heaven

while(cond){blah} > do{blah}while(cond)

4 Name: #!/usr/bin/anonymous : 2008-01-19 08:23 ID:Heaven

>>3
unless you're making a stylistic point (which is certainly valid), there's nothing wrong with post-test loop in this situation.

5 Name: #!/usr/bin/anonymous : 2008-01-20 06:34 ID:Heaven

if(Value==-9999) break;

6 Name: !TgfOVovqoo : 2008-01-20 11:47 ID:9L2KEYQv

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.

7 Name: #!/usr/bin/anonymous : 2008-01-20 16:13 ID:Heaven

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)

8 Name: #!/usr/bin/anonymous : 2008-01-20 18:27 ID:Heaven

>>7
Owned in parenthesis count by parenthesiscode. Ow.

9 Name: #!/usr/bin/anonymous : 2008-01-20 21:30 ID:Heaven

Your thread title was also kind of... shitty.

10 Name: !TgfOVovqoo : 2008-01-21 09:02 ID:9L2KEYQv

jaja use perl kekeke

11 Name: #!/usr/bin/anonymous : 2008-01-22 12:32 ID:Heaven

int sum = 0;
int value;
while ((value = d.input.readInt()) != -9999) {
if (value >0 && value <= 100) {
sum += value;
}
}

12 Name: Fix : 2008-01-22 19:36 ID:cBqVVVP+

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);
}

13 Name: #!/usr/bin/anonymous : 2008-01-23 01:53 ID:Heaven

(loop for value = 0 then (read)

  until (= value -9999)
when (<= 1 value 100) sum value)

14 Name: #!/usr/bin/anonymous : 2008-01-23 18:06 ID:Heaven

>>13 Parenthesis, gross!!

  $_ > 0 and $_ <= 100 and $sum += $_ and undef $_ while $_ = <STDIN> and $_ != -9999

15 Post deleted.

16 Post deleted.

17 Post deleted.

18 Name: #!/usr/bin/anonymous : 2008-01-23 19:58 ID:Heaven

>>14 you still need to print $sum at the end.

19 Name: #!/usr/bin/anonymous : 2008-01-24 13:51 ID:Heaven

True. Add

   or print $sum and exit

at the end

20 Name: #!/usr/bin/anonymous : 2008-01-24 18:27 ID:Heaven

"and exit" is redundant, no?

21 Name: #!/usr/bin/anonymous : 2008-01-24 19:43 ID:Heaven

but it's very poetic

This thread has been closed. You cannot post in this thread any longer.