Fix the following java code for me, and point out where I made mistakes:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
System.out.println("\n\nLet's play 20 questions. Choose an animal and I will try to guess it!");
Scanner scan = new Scanner(System.in);
code
System.out.println("Is it a mammal (y/n)?");
String answer = scan.nextLine();
if (answer.equals("y"))
{
System.out.println("Is it a pet (y/n)?");
answer = scan.nextLine();
if (answer.equals("y"))
{
System.out.println("I guess a dog! Click on run to play again.");
}
else
{
System.out.println("I guess an elephant! Click on run to play again.");
}
else {
System.out.println("Can it fly?");
String answer = scan.nextLine();
if (answer.equals("y"); {
System.out.println("I guess a bird! Click on run to play again.");
else {
System.out.println("Is it an aquarium animal?");
if (answer.equals("y"); {
System.out.println("I guess a shark!");
}
else {
System.out.println("I guess a worm!");
}
}
}
Assistant
There are several mistakes in your code that need to be fixed:
Missing closing bracket for the second if condition which checks if the animal is a pet.
Incorrect else structure – else was used as if it was an independent block. It should be associated with an if.
Redeclaration of the variable String answer within the scope where it is already declared. There's no need to declare it the second time; you can reuse the first declaration.
Use of semicolon (;) instead of a closing parenthesis ) in if conditions.
Missing closing brackets for both inner if-else blocks and for the main method block.
Here's the corrected code:
java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("\n\nLet's play 20 questions. Choose an animal and I will try to guess it!");
Scanner scan = new Scanner(System.in);
System.out.println("Is it a mammal (y/n)?");
String answer = scan.nextLine();
if (answer.equals("y")) {
System.out.println("Is it a pet (y/n)?");
answer = scan.nextLine();
if (answer.equals("y")) {
System.out.println("I guess a dog! Click on run to play again.");
} else {
System.out.println("I guess an elephant! Click on run to play again.");
}
} else {
System.out.println("Can it fly? (y/n)");
answer = scan.nextLine();
if (answer.equals("y")) {
System.out.println("I guess a bird! Click on run to play again.");
} else {
System.out.println("Is it an aquarium animal? (y/n)");
answer = scan.nextLine();
if (answer.equals("y")) {
System.out.println("I guess a shark!");
} else {
System.out.println("I guess a worm! Click on run to play again.");
}
}
}
scan.close(); // Close the scanner to prevent resource leaks
}
}
Here's a summary of what I fixed:
Corrected the placement of closing brackets.
Reorganized the else statements so they are properly associated with their corresponding if.
Removed the semicolons from the if conditions and replaced them with closing parentheses.
Added a closing bracket for the main method.
Added a call to scan.close() to close the Scanner object and prevent resource leaks. It is best practice to close the scanner when it is no longer needed.
Please note that this is a simple program that does not handle input errors (e.g., if the user does not enter 'y' or 'n') and is not flexible in terms of the range of inputs it can process (e.g., only 'y' or 'n' are considered valid inputs).