Good afternoon, everyone I have a problem with my program. My last method (isValid) checks if the input is valid from 1 to 100 when I call the method with the while loop in the first input it works fine. but in the second input it wont work you can try to enter 101 and it would continue to the 3rd input I dont know whats wrong! in the input #2 i changed the while loop to compare it to score2 which is input 2 but nothing it wont check if its a valid input
PHP Code:
import java.util.Scanner;
public class TestAverageAndLetterGrade
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int score1;
int score2;
int score3;
int score4;
int score5;
System.out.println("Enter your 1st test score: ");
score1 = keyboard.nextInt();
boolean isValid = isValid(score1);
while (isValid != true )
{
System.out.println("Enter correct Test score: ");
score1 = keyboard.nextInt();
isValid = isValid(score1);
}
System.out.println("Enter your 2nd test score: ");
score2 = keyboard.nextInt();
while (isValid != true )
{
System.out.println("Enter correct Test score: ");
score2 = keyboard.nextInt();
isValid = isValid(score2);
}
System.out.println("Enter your 3rd test score: ");
score3 = keyboard.nextInt();
while (isValid != true )
{
System.out.println("Enter correct Test score: ");
score3 = keyboard.nextInt();
isValid = isValid(score3);
}
System.out.println("Enter your 4th test score: ");
score4 = keyboard.nextInt();
while (isValid != true )
{
System.out.println("Enter correct Test score: ");
score4 = keyboard.nextInt();
isValid = isValid(score4);
}
System.out.println("Enter your 5th test score: ");
score5 = keyboard.nextInt();
while (isValid != true )
{
System.out.println("Enter correct Test score: ");
score5 = keyboard.nextInt();
isValid = isValid(score5);
}
System.out.println("=========================================");
double average = calculateAverage(score1, score2, score3, score4, score5);
System.out.println (" The average of the 5 test scores is: " + gradeLetter(average));
gradeLetter(average);
System.out.print(" test #1");
gradeLetter(score1);
System.out.print(" test #2");
gradeLetter(score2);
System.out.print(" test #3");
gradeLetter(score3);
System.out.print(" test #4");
gradeLetter(score4);
System.out.print(" test #5");
gradeLetter(score5);
}
public static double calculateAverage(int score1,
int score2,
int score3,
int score4,
int score5)
{
double average = (score1 + score2 + score3 + score4 + score5) / 5;
return average;
}
public static double gradeLetter(double average)
{
if (average>90)
{
System.out.println(" You received an A :) ");
}
else if (average>=80)
{
System.out.println(" You received a B");
}
else if (average>=70)
{
System.out.println(" You received a C ");
}
else if (average>=60)
{
System.out.println(" You received a D");
}
else if (average<60)
{
System.out.println(" You received an F :( ");
}
return average;
}
public static boolean isValid(int score)
{
boolean status;
status = true;
if(score >= 1 && score <= 100);
if(score >= 1 && score <= 100);
// if(score3 >= 1 && score3 <= 100);
// if(score4 >= 1 && score4 <= 100);
// if(score5 >= 1 && score5 <= 100);
else
status = false;
return status;
}
}