I'm trying to learn how to write code in order to find the average of numbers I enter in my program, as well as every time I enter a number it records how many I have typed in, and finally the sum of all the numbers I have entered each time. I'm wondering if someone can help me start this off, as I have no where to begin. I know my program will need to have atleast two variables, but I don't know how to implement them.

This is the code I have so far, I am having trouble implementing finding average, total, and count.

``````import java.util.*; // for class Scanner
public class Copy {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
"Enter Control-Z when done!");
double num;
while(stdin.hasNext()) {
num = stdin.nextDouble();
System.out.printf("%.2f\n", num);
}
System.out.println("Goodbye!");
System.exit(0);
}
}
``````

How would you do it manually? If I were to give you numbers one at a time (Like is done in the loop in the program)
How would you sum up the numbers keeping a running total as you got the numbers?
How would you keep track of (count) the number of numbers you were given?
I assume you know how to compute an average given a total and a count of the number of numbers.

As for the totals, I know I need to assign a variable, something like double total; or something like that? I am unsure of how after defining it to 0, how to I get the output for every time I input a number they add. ie. I put in 45, output 45, then I input 15, total goes to 60, etc:

For keeping track I believe I need to use something like count? And each time I input a number count gets +1, I know what I need, I just don't know the format, and code format to do that. I know the average will be sum/count

I'd really appreciate the help, I'm trying my best not to fail the class, but I really need someone to put my question into code context. It'll truly help a lot.

You can add a number to a running total with a simple addition eg
total = total + nextNumber;
every time you execute that witha new value for nextNumber the total will increase.

Does nextNumber need to be defined or? Because I have num in my code right now, and I don't know if that is the same thing or what.

I just used nextNumber as a variable name to help explain what was going on. It just represents whatever the number is that you want to add to total. It looks like that's the value you have called "num" in your code.
(ps in Java everything, really everything, has to be defined before it can be used)

okay i have this now, not much different, but where and what do I put in the code to keep a running total of the numbers I'm entering??

``````import java.util.*; // for class Scanner
public class Copy {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
"Enter Control-Z when done!");
double num;
double total = 0;
double average = 0;

while(stdin.hasNextDouble()) {
num = stdin.nextDouble();

System.out.printf("%.2f\n", num+total);
}
System.out.println("Goodbye!");
System.exit(0);
}
}
``````

Also what code do I write to print the total??

Are you joking?
Printing things is in the very first program you ever saw, and it's already in the code you posted above.
You should try to do a bit of research and trial-and-error yourself before asking us to give you the answer ready to submit and claim as your own homework.

I meant as in printf or println, no need to put me down. I came here to try and learn, but it's hard to learn when my professor has not taught a single thing, and expects us to know this. The book for the class is garbage, poorly written, and hard to understand.

OK.
what code do I write to print the total?
and
how can I decide whether to use println or printf?

Just remember the DaniWeb rule "Do provide evidence of having done some work yourself if posting questions from school or work assignments". If you start by demonstrating that you have thought about it and tried some ideas, then people here will help you.

Anyway, you can use any of the various versions of print, println, printf - it just depends exactly how you would like to see the output formatted. There's no one right answer. Considering where you are on the learning curve probably the simplest solution (println) would be most appropriate.

Okay after some looking around I've found how to get the count, however, I'm unsure of how to display sum and average, here's what I have:

``````import java.util.*; // for class Scanner
public class Copy {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
"Enter Control-Z when done!");
double num;
double count=0;

while(stdin.hasNextDouble()) {
num = stdin.nextDouble();
count++;

System.out.printf("%.2f\n",count);
}
System.out.println("Goodbye!");
System.exit(0);
}
}
``````

I'm not sure if there's a way but would something like total++; work or something? I just want the total, and count to be able to print out each time I'm inputting a number when the program runs, if I can figure out the total, the Average will be easy

count++ is a shorthand code for
count = count + 1;
you'll recognise that that is just an example of the code I showed you earlier:
total = total + nextNumber;

total++ will add 1 to total each time, so that's not what you need. You need to add the value of num each time. You now have all the info you need somewhere on this page.

Okay I've got this so far, I think I've got everything but It's saying something is wrong. I can't seem to obtain the total, I know the code I have is wrong for it, what do I need to be putting instead, so that it prints it out correctly?

``````import java.util.*; // for class Scanner
public class Copy {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.println("Ready to copy numbers!\nPress \"a\" when you're finished.");
double num=0, count=0, total=0;
while(stdin.hasNextDouble())
{
num+=stdin.nextDouble();
total+=stdin.nextDouble();
count++;
}
System.out.println("Average:"+num/count);
System.out.println("Count:"+count);
System.out.println("Total:"+total+num);
}
}
``````

I believe I've solved it.

``````import java.util.*; // for class Scanner
public class Copy {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.println("Ready to copy numbers!\nPress \"a\" when you're finished.");
double num=0, count=0, total=0;
while(stdin.hasNextDouble())
{
num+=stdin.nextDouble();

count++;
}
System.out.println("Average:"+num/count);
System.out.println("Count:"+count);
System.out.println("Total:"+num);
}
}
``````

count should be an int. You should never have a fractional count.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.