Assuming that the valid cost should be between 100 and 200, does the following code snippet test this condition correctly?

final int MIN_COST = 100;
final int MAX_COST = 200;
int cost = 0;
Scanner in = new Scanner(System.in);
System.out.print("Please enter the cost: ");
cost = in.nextInt();
if (cost < MIN_COST)
{
System.out.println("Error: The cost is too low.");
}
else if (cost > MAX_COST)
{
System.out.println("Error: The cost is too high.");
}
else
{
System.out.println("The cost entered is in the valid cost range.");
}

Respuesta :

Answer:

  • No, Only the value between 100 and 200 can not test the code correctly.
  • This code snippet ensures that the cost value is between 100 and 200.

Explanation:

  • The above code has three condition test- if, else-if and else.
  • The first condition is true for the value which is a minimum of 100.
  • The else if condition is true for the value which is maximum from 200.
  • Then the else condition is used when the if and else if condition is false.
  • when the user gives the value between 100 and 200 then it will test that the value lies between 100 or 200.
  • To test the whole scenario there must be one value is less than 100 and greater than 200.
ACCESS MORE