64 624
Assignments Done
99,1%
Successfully Done
In September 2018

Answer to Question #74033 in Java | JSP | JSF for Jason Hoover

Question #74033
Please if you can help me with this.. I would very much appreciate it.


import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int grade;
int nGrades = 0;
double sum = 0.0;
boolean done = false;
System.out.println("Enter grades (-1 to quit)");
while (!done) {
grade = scan.nextInt();
if (grade != -1) {
sum += grade;
nGrades++;
}
else {
done = true;
}
}
double average = sum / nGrades;
System.out.println("Average grade: " + average);
}
}


I need to be able to put grades -5 to 20
Use any other number as a sentinel value other than -5 to 20
fix NaN error that comes up if you input -1 and press enter
Expert's answer
import java.util.Scanner;

class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int grade;
int nGrades = 0;
double sum = 0.0;
double average = 0;
System.out.println("Enter grades (-1 to quit)");
do {
grade = scan.nextInt();
if (grade != -grade&&grade!=-1) {
sum += grade;
nGrades++;
average = sum / nGrades;
}
} while (grade != -1);

System.out.println("Average grade: " + average);
scan.close();
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions