java 不兼容的类型 - found:int required:boolean

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6742456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:05:35  来源:igfitidea点击:

Incompatible Types - found:int required:boolean

java

提问by JcDav

I'm trying to display: EQUIVALENT if the first numerical input is equal to the second input. What's wrong with my code?

我试图显示:如果第一个数字输入等于第二个输入,则为 EQUIVALENT。我的代码有什么问题?

import java.io.*;
public class TwoNum{
    public static void main(String[] args){
        int number;
        int number2;
        String input1="";
        String input2="";

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Input a number: ");

        try{
            input1=in.readLine();
        }catch(IOException e){
            System.out.println("Error!");
        }

        number = Integer.parseInt(input1);

        try{
            input2=in.readLine();
        }catch(IOException e){
            System.out.println("Error!");
        }

        number2 = Integer.parseInt(input2);

        if(number=number2)
        {
            System.out.println("EQUIVALENT");
        }
        if(number>number2)
        {
            System.out.println("GREATER THAN");
        }
    }
}

回答by Mark Peters

Use

利用

 if(number==number2)

Instead of

代替

 if(number=number2)

The first compares number2to numberand if they are equal evaluates to true. The second assignsthe value of number2to the variable numberand the expression evaluates to number/number2, an int.

第一个比较number2,以number和它们是否相等的计算结果为true。第二个的值分配给number2变量number,表达式的计算结果为 number/number2,一个 int。

Link

关联

回答by Ray Toal

The expression number=number2is an assignment expression producing an integer. But a boolean is expected in this context. You want ==instead of =. Common mistake.

该表达式number=number2是一个产生整数的赋值表达式。但是在这种情况下需要一个布尔值。你想要==而不是=. 常见的错误。

回答by Harry Joy

Your first condition should be:

你的第一个条件应该是:

if(number==number2)

In if condition use ==to compare 2 integers. Also don't use if in both condition use else if(). Using ifin both will check condition for both even though first condition is true it will check for second condition and you are missing 3rd condition for LESS THAN.

在 if 条件中用于==比较 2 个整数。也不要使用 if 在两种情况下都使用else if(). if在两者中使用将检查两者的条件,即使第一个条件为真,它也会检查第二个条件,而您缺少LESS THAN.