Jump to level 1
Integers numLobsters and moneyInBank are read from input. Each lobster costs 6 dollars.
Write the following if-else statement. Within the if branch, write the following assignment and nested if-else statement:
• If numLobsters is greater than or equal to 6:
1. Assign variable totalCost with the product of numLobsters and 6.
2. If totalCost is less than or equal to moneyInBank, then output "Approved transaction".
3. Otherwise, output "Not all lobsters purchased".
• Otherwise, output "Must purchase at least 6 lobsters".
End each output with a newline.
Click here for examples
Ex 1: If the input is 103 623, then the output is:
Approved transaction
Ex 2: If the input is 74 59, then the output is:
Not all lobsters purchased
Ex 3: If the input is 1 30, then the output is:
Must purchase at least 6 lobsters
moneyInBank = scnr.nextInt();
if (numLobsters >= 6){
totalCost = numLobsters * 6;
}
if (totalCost <= moneyInBank) {
System.out.println("Approved transaction");
}
else {
System.out.println("Not all lobsters purchased");
}
else {
System.out.println("Must purchase at least 6 lobsters");
}
Failed to compile
FoodPurchases.java:22: error: 'else' without 'if'
else {
1 error