Anonymous asked in Computers & InternetProgramming & Design · 8 years ago

Help with java programming?

How would you use the else and if statements?

Also can I have an example please thanks.


String s;

int grade;

s=JOptionPane.showInputDialog("Please enter your grade");


if (grade==80>);


JOptionPane.showMessageDialog(null,"You scored 80 or above, you got grade A");


else if (grade==<80&&>=60);


JOptionPane.showMessageDialog(null,"You got grade B");


What have I done wrong?

3 Answers

  • 8 years ago
    Favorite Answer

    1. When you compare, checking for "Less than or equal to", use the symbols <= in that order.

    2. When you compare, checking for "Greater than or equal to", use the symbols >= in that order.

    2. In your second "if", you don't need to test if the grade is less than 80 because you already know that from the first comparison.

    3. You need an "else" at the end to catch everything that isn't explicitly tested for.

    Here is a link to my solution.

    Source(s): I help people to learn Java programming:
  • 8 years ago

    As stated previously an if statement is as follows

    if(<logic operation>){

    //code goes here if statement above is true


    else{//<- the { is not required, however it does help with keeping the program clean.

    //Code goes here if it does not fit the if statement and is otherwise false.


    Other things I see in your code that are wrong are as follows.

    if(grade >= 80) //this says if the grade is greater than or equal to 80 do what follows

    else if(grade < 80 && grade >= 60) //This says that if the grade is less than 80 but greater than or equal to 60 do what follows

    you should also end with an else statement with an if else debate such as your above answer. Such that follows will do.


    JOptionPane.showMessageDialog(null, "Your grade is below 60");


    I hope this helps you.

    Source(s): Programmer
  • 8 years ago

    The if and else statements should start with only the {, you don't need the;

    if (grade==80>){

    The else, is just


    You don't need the rest. An else is just "do this if the if statement isn't true."

    If you need it to be specific, just create another if, instead of the else.

    I also see no need for the > and the &&, that I know of. Although I'm only an amateur.

Still have questions? Get your answers by asking now.