473,568 Members | 3,106 Online

# Count input values?

9 New Member
I'm trying to write a program that reads unspecified number of positive and negative values, counts them and computes the average of the input values, not counting zeros. The program should end with the input 0 and display the average as a floating point number.

What can I use to count the input values?

Example: If I type 1, 2 and 0 the average should be 1.5

Ps: I'm a beginner...
Sep 13 '07 #1
21 11290
r035198x
13,262 MVP
I'm trying to write a program that reads unspecified number of positive and negative values, counts them and computes the average of the input values, not counting zeros. The program should end with the input 0 and display the average as a floating point number.

What can I use to count the input values?

Example: If I type 1, 2 and 0 the average should be 1.5

Ps: I'm a beginner...
Read about the Scanner class (for reading the input) and try the program. You should make use of a while loop.
Post if you get stuck with the code.
Sep 13 '07 #2
9 New Member
Read about the Scanner class (for reading the input) and try the program. You should make use of a while loop.
Post if you get stuck with the code.
Yes I know how to read the input, but how do I count it ?
Sep 13 '07 #3
Nepomuk
3,112 Recognized Expert Specialist
Yes I know how to read the input, but how do I count it ?
How would you count them in real life? Think about that, then you'll find the answer.

PS.: Have you found out yet, if the number is positive or negative? That's the first step of course.

Greetings,
Nepomuk
Sep 13 '07 #4
r035198x
13,262 MVP
Yes I know how to read the input, but how do I count it ?
The while loop
Sep 13 '07 #5
9 New Member
I have no idea. Am I on the right track:

import java.util.Scann er;

public class Skilaverkefni2{
public static void main(String[] args) {

Scanner read = new Scanner(System. in);

System.out.prin tln("text: ");

while (number != 0) {

}

System.out.prin tln("Quit");
}
}
Sep 13 '07 #6
r035198x
13,262 MVP
I have no idea. Am I on the right track:

import java.util.Scann er;

public class Skilaverkefni2{
public static void main(String[] args) {

Scanner read = new Scanner(System. in);

System.out.prin tln("text: ");

while (number != 0) {

}

System.out.prin tln("Quit");
}
}
1.) Use code tags for posting code
2.) Try it on the compiler and see what it does or doesn't do.
Sep 13 '07 #7
9 New Member
Expand|Select|Wrap|Line Numbers
1.
2. while (number != 0) {
4. };
5. System.out.println("Quit");
This gives me the ability to enter unspecified amount of numbers until I decide to quit by entering 0 as an input.
Sep 13 '07 #8
9 New Member
Expand|Select|Wrap|Line Numbers
1.
2. while (number != 0) {
4. };
5. System.out.println("Quit");
This gives me the ability to enter unspecified amount of numbers until I decide to quit by entering 0 as an input.
I'm trying to figure out how I can add the input numbers together and get the average. Am I on the right track?
Sep 13 '07 #9
Nepomuk
3,112 Recognized Expert Specialist
I'm trying to figure out how I can add the input numbers together and get the average. Am I on the right track?
Yes, you are. Now think about how you can calculate the average - how would you do it in real life?

Greetings,
Nepomuk
Sep 13 '07 #10