This is my first semester learning any sort of Java programming and I was wondering if anyone could take a look at my code and maybe point me in the right direction and offer some feedback.
Program:
You are asked to write a Java program that reads the following 15 scores from the input device (keyboard) one at a time and prints each time the corresponding letter grades.
Scores are: 98, 78, 94, 88, 76, 56, 75, 65, 45, 57, 87, 68, 82, 90, 62, -11 (it is a sentinel value to terminate the program.)
All grades are out of 100. Grades are distributed as follows: 90-100 = A, 80-89 = B, 70 - 79 = C, 60 - 69 = D, <60 = F
Use while loop and switch to determine the letter grades for all scores. Once the sentinel value is entered, the program terminates and: 1. prints the total numbers of A, B, C, D, and F grades. 2. prints the average of the 15 scores. 3. prints the highest and lowest scores. 4. prints the number of even scores and sum of all even scores. 5. prints the number of odd scores and sum of all odd scores. "
I figured out the code for everything up to "2." I just can't figure out the code to get that to work. I feel as if I should know this and am just over thinking it. I am at a complete loss though for "4." and "5."
Thank you everyone and all help is definitely appreciated.
import java.util.Scanner;
public class Assignment4
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int score, key, scoreA = 0, scoreB = 0, scoreC = 0, scoreD = 0,
scoreF = 0;
System.out.println("Please Enter A Score: ");
score = in.nextInt();
while (score > 0 && score <= 100)
{
key = score/10;
switch (key)
{
case 10:
case 9:System.out.println("Grade = A"); scoreA++;
break;
case 8: System.out.println("Grade = B"); scoreB++;
break;
case 7: System.out.println("Grade = C");
scoreC++;
break;
case 6: System.out.println("Grade = D"); scoreD++;
break;
default: System.out.println("Grade = F"); scoreF++;
break;
}
System.out.println("Please Enter a score: ");
score = in.nextInt();
if (score < 0 || score > 100)
System.out.println("Terminated. I'll be back.");
}
System.out.println("Number of A's = " + scoreA);
System.out.println("Number of B's = " + scoreB);
System.out.println("Number of C's = " + scoreC);
System.out.println("Number of D's = " + scoreD);
System.out.println("Number of F's = " + scoreF);
int sumofGrades = (scoreA + scoreB + scoreC + scoreD + scoreF);
double averageGrade = (score / score);
double maximumGrade = Math.max(score, score);
double minimumGrade = Math.min(score, score);
double numberEven = Math.
System.out.println("The Sum of All Grades is: " + sumofGrades);
System.out.println("The average grade is: " + averageGrade);
System.out.println("The Highest Grade is: " + maximumGrade);
System.out.println("The Lowest Grade is: " + minimumGrade);
}
}
Aucun commentaire:
Enregistrer un commentaire