Advertisement
Guest User

Errors & Code Improving

a guest
May 12th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. // VERSION 1
  2.  
  3. package generalPractice1;
  4. /*
  5.  * Practice Problem:
  6.  *   - There are a ton of students and you need to determine what letter grade each student got depending on their
  7.  *     percentage.
  8.  *     * Design your code as if there were a thousand students, not just the ones provided.
  9.  *   - Print out how many of each letter grade there are using a count variable.
  10.  *   - print each student's name along with their percentage and grade letter. One student per line.
  11.  */
  12.  
  13. public class Main {
  14.     public static void main(String[] args) {
  15.         int[] grades = {30, 60, 85, 50, 90, 40, 90, 75, 99, 54};
  16.         String[] students = {"Harry", "Josh", "Kyle", "Katelyn", "Tommy", "Jackson", "Jane", "Conner", "Paul", "Nancy"};
  17.         checkGrades(grades, students);
  18.        
  19.        
  20.     }
  21.  
  22.     public static void checkGrades(int[] grades, String[] students) { // this is like functions aka modules in Python.
  23.         int countA = 0, countB = 0, countC = 0, countD = 0, countF = 0;
  24.         String pluralCountA = "", pluralCountB = "", pluralCountC = "", pluralCountD = "", pluralCountF = "";
  25.         String pluralCountA2 = "", pluralCountB2 = "", pluralCountC2 = "", pluralCountD2 = "", pluralCountF2 = "";
  26.         String letterGrade = "";
  27.        
  28.         // Determines what each student's grade is for the part that is supposed to print student name, %, and letter grade.
  29.         for (int i: grades) {
  30.             if (grades[i] >= 90) {
  31.                 letterGrade = "A";
  32.             }
  33.             else if (grades[i] >= 80) {
  34.                 letterGrade = "B";
  35.             }
  36.             else if (grades[i] >= 70) {
  37.                 letterGrade = "C";
  38.             }
  39.             else if (grades[i] >= 60) {
  40.                 letterGrade = "D";
  41.             }
  42.             else if (grades[i] <= 59) {
  43.                 letterGrade = "F";
  44.             }
  45.            
  46.             // Will go through the loop and each time run it for the corresponding student & their grade.
  47.             System.out.println(students[i] + ", " + grades[i] + ", " + letterGrade);
  48.            
  49.             // Counts how many of each letter grade there are.
  50.             if (i >= 90) {
  51.                 ++countA;
  52.             }
  53.             else if (i >= 80) {
  54.                 ++countB;
  55.             }
  56.             else if (i >= 70) {
  57.                 ++countC;
  58.             }
  59.             else if (i >= 60) {
  60.                 ++countD;
  61.             }
  62.             else if (i <= 59) {
  63.                 ++countF;
  64.             }
  65.         } // End of the for loop.
  66.        
  67.         // Determines what type of grammar to use in the println statement later on, depending on whether it is
  68.         // singular or plural.
  69.         if (countA < 2) {
  70.             pluralCountA = "is ";
  71.             pluralCountA2 = ".";
  72.         }
  73.         else {
  74.             pluralCountA = "are ";
  75.             pluralCountA2 = "s.";
  76.         }
  77.        
  78.         if (countB < 2) {
  79.             pluralCountB = "is ";
  80.             pluralCountB2 = ".";
  81.         }
  82.         else {
  83.             pluralCountB = "are ";
  84.             pluralCountB2 = "s.";
  85.         }
  86.        
  87.         if (countC < 2) {
  88.             pluralCountC = "is ";
  89.             pluralCountC2 = ".";
  90.         }
  91.         else {
  92.             pluralCountC = "are ";
  93.             pluralCountC2 = "s.";
  94.         }
  95.         if (countD < 2) {
  96.             pluralCountD = "is ";
  97.             pluralCountD2 = ".";
  98.         }
  99.         else {
  100.             pluralCountD = "are ";
  101.             pluralCountD2 = "s.";
  102.         }
  103.         if (countF < 2) {
  104.             pluralCountF = "is ";
  105.             pluralCountF2 = ".";
  106.         }
  107.         else {
  108.             pluralCountF = "are ";
  109.             pluralCountF2 = "s.";
  110.         }
  111.         System.out.println("There " + pluralCountA + countA + " A grade" + pluralCountA2);
  112.         System.out.println("There " + pluralCountB + countB + " B grade" + pluralCountB2);
  113.         System.out.println("There " + pluralCountC + countC + " C grade" + pluralCountC2);
  114.         System.out.println("There " + pluralCountD + countD + " D grade" + pluralCountD2);
  115.         System.out.println("There " + pluralCountF + countF + " F grade" + pluralCountF2);
  116.  
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement