Hi, I'm completely new to Java and having some trouble basically understanding everything. I have some code and am wondering how to edit it in order to make some questions that I'm being asked to work.

Here's the code:

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!\n" +
"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);
}
}

Here is what I'm being asked:
Modify the above program to print the
a. Count of numbers input
b. The total of the numbers input
c. The average of the numbers input

Hint: Your program will need to have 2 variables, say n and total, that keep a running count of the
number of values read and the total up to that point.

Any help would be highly appreciated, thanks so much !

Recommended Answers

All 7 Replies

Well why don't you try something. Your teacher wouldn't give you this assignment prior any lectures on Java, besides it is part of forum rules

Do provide evidence of having done some work yourself if posting questions from school or work assignments

I wouldn't post something if I hadn't tried already, I'm not looking for any freebies. I asked in my question if anyone could explain how to start it off, or where the code should be edited. I didn't ask for it to be done and given to me. My teacher gave this to us without any lectures, which is why I came here for explanation/help.

Work on the requirements one at a time:

. Count of numbers input

Define an int variable as a counter and set it to 0. Be sure to define it outside of the loop
Add one to the counter as each number is input.

okay so something like count = something? I don't really know what to put, I haven't been taught it :|

i'll look into it, thanks

There is a lot of material in the tutorial you need to read.

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.