Java GradeCalculator - While loop not working correctly -



Java GradeCalculator - While loop not working correctly -

i'm writing programme user enters number of students in class, number of exams taken in class, enters each student's names , exam scores. programme calculates student's grade , assigns them corresponding letter grade. finally, adds scores classsum, calculates average class score , displays it.

this have far: public class gradecalculator {

public static void main(string[] args) { int classsum = 0; // variable used hold sum of entire classes exams int classexams = 0; // variable used hold number of exams taken whole class scanner s = new scanner(system.in); system.out.println("welcome gradecalculator!"); system.out.println("please come in number of students:"); int students = s.nextint(); system.out.println("please come in number of exams:"); int exams = s.nextint(); int = 0; int studentnumber = 1; int sum = 0; while (i < students) { // loop until matches number of students entered above i++; sum = 0; system.out.println("enter pupil " + studentnumber++ + "'s name :"); string studentname = s.next(); system.out.println("enter exam scores :"); int input = 0; (; input < exams; input++) { int n = s.nextint(); sum+=n; if (n < 0) { system.out.println("invalid exam scores, reenter: "); //if 1 of scores entered negative, display message } } double average = sum/exams; // assign letter grade based on average of exams if (average <= 100 && average >= 90) { system.out.println("letter grade: a"); system.out.println(studentname + " gets 4 stars! ****"); } if (average <= 89 && average >= 80) { system.out.println("letter grade: b"); system.out.println(studentname + " gets 3 stars! ***"); } if (average <= 79 && average >= 70) { system.out.println("letter grade: c"); system.out.println(studentname + " gets 2 stars! **"); } if (average <= 69 && average >= 60) { system.out.println("letter grade: d"); system.out.println(studentname + " gets 1 star! *"); } if (average <= 59) { system.out.println("letter grade: f"); system.out.println(studentname + " gets 0 stars!"); } classsum += sum; // add together sum of student's scores classsum classexams += exams; // add together exams taken pupil amount of exams taken whole class } int classaverage = classsum/classexams; // compute class average system.out.println("class statistics:"); system.out.println("\taverage: " + classaverage); } }

and output get:

welcome gradecalculator! please come in number of students: 2 please come in number of exams: 3 come in pupil 1's name : sam come in exam scores : 80 80 80 letter grade: b sam gets 3 stars! *** come in pupil 2's name : joe come in exam scores : 90 90 90 class statistics: average: 125

as can see, after first student, entering name , exam scores not display next student's letter grade loop supposed do. also, class average not beingness calculated correctly. i'm @ bit of loss here i'm going wrong.

you have set sum 0 each turn of loop

while (i < students) { // loop until matches number of students entered above i++; sum = 0; system.out.println("enter pupil " + studentnumber++ + "'s name :"); string studentname = s.next(); // [...]

java while-loop

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -