Reputation: 1
I have been trying to figure this out, and either I'm thinking about it too much or I just am plain missing something. I need the user to input the number of students, got that. Then I need to enter a single test grade for each student based on the number the user entered. This is where I'm stuck. I then need to calculate the sum based on those test grades, and calculate the average. Any help would be greatly appreciated. I have this so far:
import java.util.*;
public class computeSumAverage {
public static void main(String[] args) {
Scanner kbd = new Scanner(System. in );
System.out.print("Please enter the number of students = ");
int student = kbd.nextInt();
System.out.print("Please enter " + student + " test grades = ");
int count;
double grades = kbd.nextDouble();
double sum = kbd.nextDouble();
double average;
for (count = 0; count <= 100; count++);
sum = (grades++);
average = (sum / student);
System.out.println("The sum of the numbers is = " + sum + "." + "\n");
System.out.println("The average of the numbers is = " + average + ".");
}
}
Upvotes: 0
Views: 1798
Reputation:
Try this. It should work.
public static void main (String[] args) {
Scanner kbd = new Scanner (System.in);
System.out.print ("Please enter the number of students = ");
int count = kbd.nextInt();
double sum = 0;
double average = 0;;
for (int student = 0; student < count; student ++){
System.out.print ("Please enter Student: " + String.valueOf(student +1) + " test grades = ");
double grades = kbd.nextDouble();
sum += grades;
}
average = (sum / count);
System.out.println ("The sum of the grades is = " + sum + "." + "\n");
System.out.println ("The average of the grades is = " + average + ".");
}
What is wrong with your code.
in the for loop is useless because it does not help you achieve what your trying to do and the ; stops it right away. There is no core to the loop. You should loop for the number of students that you have.
Inside the loop you should read the grades and prompt for the user to enter the grades for each student.
The sum well you didn't really try I guess.
You should think before coding on what you are doing.
Upvotes: 2
Reputation: 26
A few things are missing from your solution:
For example:
public static void main(String[] args) {
Scanner kbd = new Scanner(System. in );
System.out.print("Please enter the number of students = ");
int students = kbd.nextInt();
System.out.print("Please enter " + students + " test grades = ");
double sum = 0;
for (int count = 0; count < students; count++) {
double grade = kbd.nextDouble();
sum += grade;
}
double average = (sum / students);
System.out.println("The sum of the numbers is = " + sum + "." + "\n");
System.out.println("The average of the numbers is = " + average + ".");
}
Upvotes: 0
Reputation: 2670
You got number of students into student
and after that you should ask for grades within a loop ( for
loop for example ) each student as many times as many students you have, but you are asking for grade outside of loop only once ...
Please read up about for () {} and what { and } are used for.
Working snippet is:
...
double sum = 0;
for ( int i = 1; i <= students; i++ ) {
System.out.println("Grade for student " + i ":");
double grad = kdb.nextDouble();
sum = sum + grade;
}
double avg = sum / students;
...
Upvotes: 0
Reputation: 271
This line:
for (count = 0; count <= 100; count ++);
should be:
for (count = 0; count <= 100; count ++)
see: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
Upvotes: 0