-1

I can't seem to get my program to loop properly. So, the goal is that after the user input their numerical grade, it will output the corresponding letter grade. After that, the user is prompted whether or not they would like to go again, in which they can enter "y" or "Y" to continue. At this point, however, even when entering "y" or "Y", the program will not loop.

//Grades.java by Jonathan Holter 02/01/2015

import java.util.*;
public class Grades
{
public static void main(String args[]) 
{
    int numGrade = 0;
    String againRun = "y", letterGrade = "A";

    Scanner keyboard = new Scanner(System.in);
    System.out.print("\nWelcome to Jonathan Holter's Grade Converter" + "\n--------------------------------------------");

    while(againRun == "Y" || againRun == "y")
    {
        do
        {
            System.out.print("\n\nEnter the numerical grade: ");
            numGrade = keyboard.nextInt();
            keyboard.nextLine();

            if(numGrade < 0 || numGrade > 100)
            {
                System.out.print("\nERROR: Out of Range" + "\nPlease choose a value between 0 - 100");
            }
        }
        while(numGrade < 0 || numGrade > 100);

        if(numGrade < 60)
        {
            letterGrade = "E";
        }

        else if(numGrade > 59 && numGrade < 70)
        {
            letterGrade = "D";
        }

        else if(numGrade > 69 && numGrade < 80)
        {
            letterGrade = "C";
        }

        else if(numGrade > 79 && numGrade < 90)
        {
            letterGrade = "B";
        }

        else if(numGrade > 89 && numGrade < 101)
        {
            letterGrade = "A";
        }

        System.out.print("\nLetter Grade: " + letterGrade);

        System.out.print("\n\nContinue? <Y/N> ");
        againRun = keyboard.next();

    }


}

}

user3490404
  • 1
  • 1
  • 3

2 Answers2

0

When comparing strings use String.equals(String) not String == String. String.equals compares the contents of the string, String == String compares if the strings are the same object.

Moddl
  • 360
  • 3
  • 13
0

Use the .equals() method of a string to compare the contents of the string. Using == will compare if they are the same instance of the same object.

See here for more information.

Community
  • 1
  • 1
jnd
  • 754
  • 9
  • 20