Using if else statement in NetBeans

January 31, 2016 at 08:18:38
Specs: Windows 7, 4GB
Hello, any suggestions how do I use the if else statement? Here's my code currently:

import java.util.Scanner;


public class AverageGrade {

private static float quit;
public static void main(String[] args) {
System.out.println("Enter a grade: ");
Scanner scan = new Scanner(System.in);
float num1 = scan.nextFloat();
System.out.println("Enter a grade: ");
float num2 = scan.nextFloat();
System.out.println("Enter a grade: ");
float num3 = scan.nextFloat();
System.out.println("Enter a grade: ");
float num4 = scan.nextFloat();

System.out.println("Please enter quit or a valid number");
int text2 = scan.nextInt();


if ( text2 == quit ){
float num5 = num1 + num2 + num3 + num4;
System.out.println("The total is " + num5);
float average;
average = num5 /4;
System.out.println("Your average is " + average);

}
else {
System.out.println("You have entered an invalid number");
}


}
}

Before the if else statement, I want it to prompt "Please enter quit or a valid number". After that, the total and average will only load when the word "quit" is typed. Otherwise, it will show "You have entered an invalid number". The error now is somewhere in the if else statement.


See More: Using if else statement in NetBeans

Report •


#1
May 4, 2016 at 03:22:06
I didn't understand what you want to do.

Report •

#2
May 4, 2016 at 03:39:21
Okay, I get it now. When you put the if (text2 == quit) the system recognized it as: if the text2 is equal to the WORD quit, do whatever you want to do.
So briefly, as long as you are entering grades which have no negative values, you can use it like this: if (text2 >= 0)
The system just takes it as a positive number, even if you enter a character it will display the else statement. Hope it's helpful.

Report •

Related Solutions


Ask Question