不兼容的类型 - 找到:int required:boolean [英] Incompatible Types - found:int required:boolean

查看:1512
本文介绍了不兼容的类型 - 找到:int required:boolean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示:EQUIVALENT,如果第一个数字输入等于第二个输入。我的代码出了什么问题?

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

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");
        }
    }
}


推荐答案

使用

 if(number==number2)

而不是

 if(number=number2)

第一个比较 number2 数字如果等于,则评估为 true 。第二个 number2 的值分配给变量 number ,表达式求值为数字/ number2,一个int。

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

  • Summary of Operators (The Java Tutorials)

这篇关于不兼容的类型 - 找到:int required:boolean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆