mercredi 31 décembre 2014

Not understanding how to make my for loop and if statement work

I have this, but I'm very lost in how I can get the final GPA to print out, I've tried various ways but have not been able to do it successfully. This is what I have:



Scanner input = new Scanner(System.in);

System.out.println("How many grades are you putting? ");
int length = input.nextInt();
input.nextLine();

String[] gradesArray = new String[length];

for(int i = 0; i < gradesArray.length; i++)
{
System.out.println("Enter grade (include + or -) ");
gradesArray[i] = input.nextLine();

double points = 0.0;
if(gradesArray[i].equalsIgnoreCase("A+") || gradesArray[i].equalsIgnoreCase("A"))
{
points += 4;

}
else if(gradesArray[i].equalsIgnoreCase("A-"))
{
points+= 3.7;

}
else if(gradesArray[i].equalsIgnoreCase("B+"))
{
points += 3.3;

}
else if(gradesArray[i].equalsIgnoreCase("B"))
{
points += 3.0;

}
else if(gradesArray[i].equalsIgnoreCase("B-"))
{
points += 2.7;

}
else if(gradesArray[i].equalsIgnoreCase("C+"))
{
points += 2.3;

}
else if(gradesArray[i].equalsIgnoreCase("C"))
{
points += 2.0;

}
else if(gradesArray[i].equalsIgnoreCase("D"))
{
points += 1.0;

}
else if(gradesArray[i].equalsIgnoreCase("F"))
{
points += 0.0;

}
else
{
System.out.println("Invalid grade");
}
System.out.println("GPA: " + points / gradesArray.length);
}


I'm guessing the GPA does not print out properly because after the condition matches the grade, it then goes right down to print, right? And also, how can I do it so if they enter an invalid grade, it makes the user start over.


Aucun commentaire:

Enregistrer un commentaire