0

I'm fairly new to java coding but the error is that if I type in the correct answer or the incorrect answer, it still says both Correct and Incorrect.

import java.util.Scanner;
public class MathTest {
    public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);
        System.out.println("What is 5 times 4? ");
        String question;
        question = user_input.next();
        if (question.equals(20));
        System.out.println("Correct!");
        if (!"20".equals(question));
        System.out.println("Incorrect!");
    }
}
0

6 Answers 6

2

Remove the semi-colons from the end of your if statements. It's treated as an empty line.

Or even use explicit curly braces, like if(foo) { bar(); }.

Sign up to request clarification or add additional context in comments.

1 Comment

equals(20) should also be be equals("20").
1

Your conditional if's are a little wrong, this should do it: (untested)

import java.util.Scanner;
public class MathTest {
    public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);
        System.out.println("What is 5 times 4? ");
        String question;
        question = user_input.next();
        if (question.equals("20"))
            System.out.println("Correct!");
        if (!"20".equals(question))
            System.out.println("Incorrect!");
    }
}

In your original code, you had ; at the end of the if's. Which informs the compiler that it's the end of the line. Hence why both "Correct!" and "Incorrect!" were being displayed. AS they weren't part of the conditional statement.

Hope this helps! :)

Comments

1

Find your solutions Brother....... Tip: for String use "20" instead of 20

public class Solution {

    public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);
        System.out.println("What is 5 times 4? ");
        String question;
        question = user_input.next();
        if (question.equals("20")){
        System.out.println("Correct!");
        }else{
              System.out.println("Incorrect!");             
        }
    }

}

Comments

1

Semicolon ends the if statement, so the statements below each if will always execute. In addition this: if (question.equals(20)) should be if (question.equals("20")) as the user input is String and you want to compare Strings.

The code should read as follows:

public class MathTest {

    public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);
        System.out.println("What is 5 times 4? ");
        String question;
        question = user_input.next();
        if (question.equals("20")) {
            System.out.println("Correct!");
        }
        if (!"20".equals(question)) {
            System.out.println("Incorrect!");
        }
    }
}

It is worth mentioning that the Scanner has other methods to read user input, e.g. you can read Integer instead of String like this: Integer answer = answer = user_input.nextInt(); And then you just compare Integers like:

if (answer == 20) {
    System.out.println("Correct!");
} else {
    System.out.println("Incorrect!");
}

Comments

0

That is because you have a semicolon after the if. Remove it, and preferably put curly braces around the println statements to show which statements belong to the if. Right now, you have written

if (...); a;

Which is equivalent to

 if (...)
 {
 }

 a;

While what you meant is

if (...)
{
    a;
}

Comments

0

Make it simple

    Scanner user_input = new Scanner(System.in);
    System.out.println("What is 5 times 4? ");
    System.out.println(user_input.next().equals("20") ? "Correct" : "Incorrect");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.