Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why doesn't this work?

 
william roberts
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi it's me again, the java absolute novice. Can someone please tell me why the little program below doesn't work. It's supposed to sum the positive numbers from ten inputs and to discount any negatives.

forgive me if it's a problem for four year olds, but I'm only two!!


[This message has been edited by Cindy Glass (edited September 27, 2001).]
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi William -
Thing are a little mixes up here; first, don't assign total a value from the console; just initialize it to 0 at the beginning of your main(). Second, change your if test to (num > 0).

------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Aaron Wilt
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I'm sort of new to java too, but I'll give it a shot. It seems like to me that you have some of the workings there, just kind of in a different order. This is how I would do it...
// skiped class def
int total, num, counter; // declare vars
//start for loop, go from 1 to 10
for(counter = 1; counter <= 10; counter++) { <br /> System.out.println("Enter a number"); //prompt user<br /> num = Course_io.readInt(); //read in their int<br /> if(num >= 0) //check to see if num is non-negative
total += num; //if it is, add it to total
} // end of for
System.out.println("The total is " + total); //print total
Note: total += num; is the same as total = total + num. Here we are only checking to see if it is non negative, we don't need an else because when the "if statement" is untrue, it will skip the addition to total. then you hit the closing brace for the "for loop" and it bounces back up to the top incrementing our counter.
Hope this helps!
[This message has been edited by Aaron Wilt (edited September 27, 2001).]
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Williams,
A couple of things.
First, your for() loop syntax is wrong. You need to use a ';' to separate the individual parts of the for() loop. E.g. for( ; ; ).
Second, I think you want to execute several statements inside the for() loop, so you'll need to put a pair of { } around those statements.
To increment the counter, the standard idiom is to use the incremental operator. E.g. counter++ or ++counter.

I just have to ask, where did you get Course_io? I'm assuming this is some io class from the course you're taking.
-Peter
 
william roberts
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Peter Tran - bartender.
Thanks for your help. In answer to you question Course.io was downloaded from: http://www.mrjo.demon.co.uk/downloads/jdk/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic