-2

Not quite sure what is wrong with this. I've tried a do-while and a switch as well, but nothing seems to be working. Is there something I'm missing? When the code executes and the user enters Y to continue, it just ends, but I need it to go back to the beginning so that the user can input another grade.

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("Enter your grade: ");
    double grade = input.nextDouble();
    while (grade >=0 &&grade <=100)
        if(grade<=100&&grade>=90)
        {
            System.out.println("Your grade is an A"); break;
        } else if (grade <=89&&grade>=80){
            System.out.println("Your grade is a B"); break;
        } else if (grade <=79&&grade>=70) {
            System.out.println("Your grade is a C"); break;
        } else if (grade <=69&&grade>=60) {
            System.out.println("Your grade is a D"); break;
        } else if (grade <=59) {
            System.out.println("Your grade is an F");break;
        }    
        System.out.println("Do you want to continue (Y/N)?");    
        char check = input.next().charAt(0);    
    }

}
Rao
  • 20,781
  • 11
  • 57
  • 77
Taylor
  • 13
  • 3
  • 6
    Read your own code. When does the loop stop or continue? Does it has any relationship with the value of `check`, containing what the user entered? Also what do you think `break;` does? – JB Nizet Feb 15 '16 at 19:10
  • 7
    It looks like you're missing the braces for the `while` loop. In java if you don't provide braces, it automatically assumes the next statement is it's only contents. This means that your loop doesn't contain anything but the `if-else-if` statement. – Khalos Feb 15 '16 at 19:11
  • According to the code you posted, you should never get to the line that asks the user if they want to continue unless you enter a negative number or something greater than 100. – Brian Feb 15 '16 at 19:16
  • I don't know JB. I don't code in Java. I code in C#. Could you perhaps be a little more polite and a little more informative? Or am I asking too much? – Taylor Feb 16 '16 at 05:04
  • Brian, the code was given to me by my professor to fix. He doesn't know what he's talking about and neither do I. I code predominantly in C# for work, and have no Java experience. It works up to that point. – Taylor Feb 16 '16 at 05:05

4 Answers4

3

You may want to declare char check = 'Y' before the while and then enclose the statements after the while into curly braces like this:

while (check =='Y' && grade >=0 && grade <=100) { 
... 
}

Also, you need to remove the break; statements since it will exit from the loop.

JFPicard
  • 5,029
  • 3
  • 19
  • 43
  • This is definitely part of the problem. @JB Nizet has the other half. `break;` will break out of the loop even after adding the brackets. – Khalos Feb 15 '16 at 19:15
  • Even after adding braces Taylor didn't use the input check – Nikitha Feb 15 '16 at 19:15
  • As I mentioned before, I was given this code by my professor to fix, and I have never coded in Java before. The break was necessary because if it wasn't there, it would continually run and keep repeating the output. – Taylor Feb 16 '16 at 05:09
1

The problem with your current implementation is that first of all, you don't let the user insert more than one grade, by breaking each if statement.

Back to the initial words, the user input, regarding resuming with grades' insertion isn't handled inside a loop. A do-while loop should be used, instead.

public static void main(String[] args) {

 boolean flag = false;
 Scanner input = new Scanner(System.in);


 double grade;
 do {
  System.out.println("Enter your grade: ");
  grade = input.nextDouble();
  if (grade <= 100 && grade >= 90) {
   System.out.println("Your grade is an A");
  } else if (grade <= 89 && grade >= 80) {
   System.out.println("Your grade is a B");
  } else if (grade <= 79 && grade >= 70) {
   System.out.println("Your grade is a C");
  } else if (grade <= 69 && grade >= 60) {
   System.out.println("Your grade is a D");
  } else if (grade <= 59) {
   System.out.println("Your grade is an F");
  }

  System.out.println("Do you want to continue (Y/N)?");
  char check = input.next().charAt(0);
  if (check == 'Y') {flag = true;}
  else {flag = false;}
 } while (grade >= 0 && grade <= 100 && flag == true);
}

Generally, the above (breaking if-else statements) is considered a bad practice and I foresee that you confused the usage of switch statement with the if-else.

On a switch statement, break is used as we only want one of the examined conditions to be satisfied. In case of break absence, once a match is made to a case, execution will continue to the rest of the remaining cases that are written below the matched case.

On the other hand, the if-else code will only enter the statement that matches and will not execute any other if-else statement, until the end of the last if-else (or else) statement, so, there's no need to use break here, as the compiler itself knows that if it makes a match on an if statement, it will then move after the end of that block, after having executing the matched if (or if-else or else) statement.

And this is for you further reference, regarding my fore-mentioned words.

Community
  • 1
  • 1
teobais
  • 2,820
  • 1
  • 24
  • 36
  • Thank you for your polite response. I didn't realize I would be getting torn up here. I appreciate your explanation and code example, and it was really helpful. That did was I wanted it to do. Thank you. – Taylor Feb 16 '16 at 05:10
0

Maybe try this one:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    while(true) {
        System.out.println("Enter your points: ");
        double grade = input.nextDouble();

        if(grade >= 0 && grade <= 100) {
            if(grade >= 90)
            {
                System.out.println("Your grade is an A");
            }
            else if(grade >= 80)
            {
                System.out.println("Your grade is a B");
            }
            else if(grade >= 70)
            {
                System.out.println("Your grade is a C");
            }
            else if(grade >= 60)
            {
                System.out.println("Your grade is a D");
            }
            else
            {
                System.out.println("Your grade is an F");
            }
        } else {
            System.out.println("Only points between 0 and 100 are allowed.");
        }

        char check;
        do {
            System.out.println("Do you want to calculate another grade (Y/N)?");
            check = input.next().charAt(0);
        } while(Character.toUpperCase(check) != 'Y' && Character.toUpperCase(check) != 'N');
        if(Character.toUpperCase(check) == 'N')
            break;
    }
}

You can easily use an infinite loop here, as long as you add an appropriate exit point like I did with the last check.

It is not necessary to check both bounds for each class of points if the order of the if-statements is correct. The last else-if is also not necessary and a possibly dangerous, because in case of a bound error you would not get any output for certain input(s).

I also added Character.toUpperCase() for the Y/N question, as it is quite convenient in my opinion.

Namoshek
  • 6,394
  • 2
  • 19
  • 31
  • Thank you so much. This makes a lot more sense than what I was trying to do and it works a lot better too! Thanks for explaining it a little too. – Taylor Feb 16 '16 at 05:11
  • You should really avoid while(true) {...} construct. It's a recipe for never ending loops. – JFPicard Feb 16 '16 at 13:55
0

As a general rule of thumb, don't 'break;' out of loops. Your loop's controls and conditionals should be strong enough that you do not need to use break.

I know your solution has already been solved, but give a man a fish, teach a man to fish... you know the rest.

let's walk through your code.

  • Initialize Scanner for input
  • prompt user for input
  • store user input in a double (doing great so far!)
  • user enters 85
  • grade is within acceptable parameters, let's enter the while loop! (don't forget that first curly brace!)

    while (expression) {}

  • 1st condition = false so skip to 2nd condition (grade <=89&&grade>=80) PASS!

  • program prints - "Your grade is a B" break;

  • program exits with code 0.


what just happened? I swore I told it to loop! break happened sucka! breaks will force what ever process you are running to stop. In this case the loop. continue; will skip the rest of the code in the loop and restart the loop at the next iteration. Truth is, neither of those are right for your code.

the other problem with your solution here is that there is no way of continuing the loop as nothing feeds back to the top. At this point you should prompt the user for input (do you want to continue y/n)

if (y) please enter another grade
  grade = input.nextDouble();

else
  grade = -1;

by setting your grade to -1 you will make while (grade >=0 &&grade <=100) evalutate to false thus exiting your loop in a more natural and easier to read way.

I think that the important thing to take away here is that you avoid using break anywhere that is unnecessary. You'll save your self a lot of pain later.