Ok I don't know if I can delete my old post but http://www.javaprogrammingforums.com...050-loops.html is useless I figured out that it wasn't a problem with my loop
I wrote this code twice once with arrays and once by doing a bunch of if statements because I thought maybe the array was causing the problem.
BUT, the problem I am getting with both examples is that I can't get the variable that I calculated inside of the if statements out or out of the for loop, and idk how to do this I have been looking at my book and googling just to see if I could find a way, but I can't so I come to you.
If Statement's
import java.util.Scanner; public class ChastainJosephCalculateGrades1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double Assignment1; double Assignment2; double Assignment3; double Assignment4; double Assignment5; double Assignment6; double AssignmentAvg; double Test1; double Test2; double TestAvg; double Final; double Lab; double Avg; String Input; System.out.println("Would you like to calculate your grade?(y/n)"); Input = sc.nextLine(); while(Input.equals("y") || Input.equals("Y")) { System.out.print("What was your first assignment grade?"); Assignment1 = sc.nextDouble(); System.out.print("What was your second assignment grade?"); Assignment2 = sc.nextDouble(); System.out.print("What was your third assignment grade?"); Assignment3 = sc.nextDouble(); System.out.print("What was your fourth assignment grade?"); Assignment4 = sc.nextDouble(); System.out.print("What was your fifth assignment grade?"); Assignment5 = sc.nextDouble(); System.out.print("What was your sixth assignment grade?"); Assignment6 = sc.nextDouble(); System.out.print("What was your first test grade?"); Test1 = sc.nextDouble(); System.out.print("What was your second test grade?"); Test2 = sc.nextDouble(); System.out.print("What was your lab grade?"); Lab = sc.nextDouble(); System.out.print("What was your final exam grade?"); Final = sc.nextDouble(); if(Assignment1 < Assignment2 && Assignment1 < Assignment3 && Assignment1 < Assignment4 && Assignment1 < Assignment5 && Assignment1 < Assignment6) { AssignmentAvg = (Assignment2 + Assignment3 + Assignment4 + Assignment5 + Assignment6) / 5; }else if(Assignment2 < Assignment1 && Assignment2 < Assignment3 && Assignment2 < Assignment4 && Assignment2 < Assignment5 && Assignment2 < Assignment6) { AssignmentAvg = (Assignment1 + Assignment3 + Assignment4 + Assignment5 + Assignment6) / 5; }else if(Assignment3 < Assignment2 && Assignment3 < Assignment1 && Assignment3 < Assignment4 && Assignment3 < Assignment5 && Assignment3 < Assignment6) { AssignmentAvg = (Assignment2 + Assignment1 + Assignment4 + Assignment5 + Assignment6) / 5; }else if(Assignment4 < Assignment2 && Assignment4 < Assignment3 && Assignment4 < Assignment1 && Assignment4 < Assignment5 && Assignment4 < Assignment6) { AssignmentAvg = (Assignment2 + Assignment3 + Assignment4 + Assignment5 + Assignment6) / 5; }else if(Assignment5 < Assignment2 && Assignment5 < Assignment3 && Assignment5 < Assignment4 && Assignment5 < Assignment1 && Assignment5 < Assignment6) { AssignmentAvg = (Assignment2 + Assignment3 + Assignment4 + Assignment1 + Assignment6) / 5; }else if(Assignment6 < Assignment2 && Assignment6 < Assignment3 && Assignment6 < Assignment4 && Assignment6 < Assignment5 && Assignment6 < Assignment1) { AssignmentAvg = (Assignment2 + Assignment3 + Assignment4 + Assignment5 + Assignment1) / 5; } TestAvg = (Test1 + Test2) / 2; Avg = (AssignmentAvg * .25) + (TestAvg * .30) + (Lab * .25) + (Final * .20); if(Avg >= 90) { System.out.println("Your grade is an A, and your average is " + Avg + "."); }else if(Avg >= 80) { System.out.println("Your grade is a B, and your average is " + Avg + "."); }else if(Avg >= 70) { System.out.println("Your grade is a C, and your average is " + Avg + "."); }else if(Avg >= 60) { System.out.println("Your grade is a D, and your average is " + Avg + "."); }else if(Avg < 60) { System.out.println("Your grade is an F, and your average is " + Avg + "."); } System.out.println("Do you want to calculate the grade of another student?"); Input = sc.nextLine(); } System.out.println("Thank you for using my Average Calculator, Bye bye!!."); } }
Error:
C:\Users\Joseph Chastain\Desktop\CS Class\Assignments\ChastainJosephCalculateGrades1.j ava:83: error: variable AssignmentAvg might not have been initialized
Avg = (AssignmentAvg * .25) + (TestAvg * .30) + (Lab * .25) + (Final * .20);
Arrays
import java.util.Scanner; public class ChastainJosephCalculateGrades { public static void main(String[] args) { Scanner sc = new Scanner(System.in); final int Assignments = 6; final int Tests = 2; double AssignmentAvg; double TestAvg; int Final; int Lab; double Avg; int[] AGrade = new int[Assignments]; int[] TGrade = new int[Tests]; System.out.println("Would you like to calculate your grades now?(y/n)"); String Input = sc.nextLine(); while(Input.equals("y") || Input.equals("Y")) { System.out.println("Enter your" + Assignments + " assignment grades."); for(int index = 0; index < Assignments; index++) { System.out.print("Assignment #" + (index + 1) + ": "); AGrade[index] = sc.nextInt(); double lowest = AGrade[0]; if(AGrade[index] < lowest) { lowest = AGrade[index]; } AssignmentAvg = (AGrade[index] - lowest) / Assignments; } System.out.println("Enter your" + Tests + " test grades."); for(int index = 0; index < Tests; index++) { System.out.print("Test #" + (index + 1) + ": "); TGrade[index] = sc.nextInt(); TestAvg = TGrade[index] / Tests; } System.out.println("Enter your lab grade."); Lab = sc.nextInt(); System.out.println("Enter your final grade."); Final = sc.nextInt(); Avg = (AssignmentAvg * .25) + (TestAvg * .30) + (Lab * .25) + (Final * .20); if(Avg >= 90) { System.out.println("Your grade is an A, and your average is " + Avg + "."); }else if(Avg >= 80) { System.out.println("Your grade is a B, and your average is " + Avg + "."); }else if(Avg >= 70) { System.out.println("Your grade is a C, and your average is " + Avg + "."); }else if(Avg >= 60) { System.out.println("Your grade is a D, and your average is " + Avg + "."); }else if(Avg < 60) { System.out.println("Your grade is an F, and your average is " + Avg + "."); } System.out.println("Do you want to calculate the grade of another student?"); Input = sc.nextLine(); } System.out.println("Thank you for using my Average Calculator, Bye bye!!"); } }
Error:
C:\Users\Joseph Chastain\Desktop\CS Class\Assignments\ChastainJosephCalculateGrades.ja va:59: error: variable AssignmentAvg might not have been initialized
Avg = (AssignmentAvg * .25) + (TestAvg * .30) + (Lab * .25) + (Final * .20);
C:\Users\Joseph Chastain\Desktop\CS Class\Assignments\ChastainJosephCalculateGrades.ja va:59: error: variable TestAvg might not have been initialized
Avg = (AssignmentAvg * .25) + (TestAvg * .30) + (Lab * .25) + (Final * .20);