简体   繁体   English

比较用户输入和整数

[英]Compare user input with integer

I'm making a program where it asks the user a simple math question and it has to tell them whether they are correct or incorrect. 我正在编写一个程序,向用户询问一个简单的数学问题,它必须告诉他们是正确的还是错误的。 I'm getting errors 我遇到错误

package exercises;

import java.util.Scanner;
public class Exercises {


    public static void main(String[] args) {
        Scanner user_input = new Scanner (System.in);
        String number;
        System.out.println("Consider the following equation: 5x - 10");
        System.out.println("Solve for x.")
        number = user_input.next();

        if (number == 2);{
            System.out.println("Correct.");

        }else{
            System.out.println("Incorrect. The answer is 2.");
        }

}

Your code has two major problems and one minor. 您的代码有两个主要问题和一个小问题。


Semicolon in if 如果使用分号

First one is the semicolon right after the if-statement. 第一个是if语句后的分号。 The semicolon finishes the if-statement, it is short for the if-statement without curly braces, like: 分号完成if语句,它是不带花括号的if语句的缩写,例如:

if (a > b) doMethod();

By omitting an expression and only writing the semicolon you represent a valid NOP (no operation), so 通过省略表达式并仅写分号,您可以表示有效的NOP (无操作),因此

if (a > b) ;

is valid statement which basically does nothing. 是有效的语句,基本上不执行任何操作。

You probably intended 你可能打算

if (number == 2) {
    System.out.println("Correct.");
} else {
    System.out.println("Incorrect. The answer is 2.");
}

Comparison 比较方式

The other problem is that your number variable is a String but you compare it with an int . 另一个问题是您的number变量是String但是将其与int进行了比较。 That won't work, the result will always be false . 那行不通,结果永远是false You will need to convert either the String to int or vice versa for the comparison to work. 您需要将String转换为int ,反之亦然,以进行比较。

Note that comparing String with String using == does not work as you might expect (see How do I compare strings in Java? ) for details, use String#equals instead. 请注意,使用==StringString比较无法正常工作(请参见如何在Java中比较字符串? )以获取详细信息,请改用String#equals

So one possibility would be String with String : 所以一种可能是StringString

String number = user_input.next();

if (number.equals("2")) {

The other is int with int : 另一个是intint

String number = user_input.next();
int asValue = Integer.parseInt(number);

if (asValue == 2) {

or directly use Scanner#nextInt : 或直接使用Scanner#nextInt

int number = user_input.nextInt();

if (number == 2) {

Missing semicolon 缺少分号

The minor problem is that you forgot a semicolon after the following statement 较小的问题是您在执行以下语句后忘记了分号

System.out.println("Solve for x.") // Semicolon needed

In Java every expression must end with a semicolon, it is a strict language. 在Java中,每个表达式都必须以分号结尾,这是一种严格的语言。

You have two syntax errors and both are related to semicolon ; 您有两个语法错误,都与分号有关; .

The first error is this line System.out.println("Solve for x.") . 第一个错误是这行System.out.println("Solve for x.") In Java every statement must end with ; 在Java中,每个语句必须以;结尾; . This line has to be System.out.println("Solve for x."); 这行必须是System.out.println("Solve for x."); .

The second error is in if statement: if (number == 2);{ . 第二个错误是在if语句中: if (number == 2);{ You should remove semicolon after close parentheses. 您应该在小括号后删除分号。 The correct line of code is if (number == 2) { . 正确的代码行是if (number == 2) {

You are trying to compare number which is a String, with an integer and it can't be done, here you have two option 您正在尝试将number它是一个字符串)与一个整数进行比较,这是不可能完成的,这里有两个选择

1) read an integer from System.in 1)从System.in读取一个整数

public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);
        String number;
        System.out.println("Consider the following equation: 5x - 10");
        System.out.println("Solve for x.");
        number = user_input.next();

        if (Integer.parseInt(number) == 2) {
            System.out.println("Correct.");

        } else {
            System.out.println("Incorrect. The answer is 2.");
        }
    }

2) parse the number as integer before comparing it with the int 2)将number解析为整数,然后将其与int进行比较

public static void main(String[] args) {
        Scanner user_input = new Scanner(System.in);
        int number;
        System.out.println("Consider the following equation: 5x - 10");
        System.out.println("Solve for x.");
        number = user_input.nextInt();

        if (number == 2) {
            System.out.println("Correct.");

        } else {
            System.out.println("Incorrect. The answer is 2.");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM