繁体   English   中英

类型不匹配:无法从 int 转换为 boolean

[英]Type mismatch:could not convert from int to boolean

if语句中,在参数中,我收到一条错误消息,指出“类型不匹配,无法从 int 转换为布尔值”。 请提供解决方案。

public static void main(String[] args) {

    Scanner sathya1 = new Scanner(System.in);
    System.out.println("Enter the numbers");
    int x = (sathya1.nextInt());
    int y = (sathya1.nextInt());
    int addition = x+y;
    int subtraction = x-y;
    int multiplication = x*y;
    float division = x/y;       
    if(sathya1.nextInt(addition){
        System.out.println("The number is " +addition);
        elseif(sathya1.nextInt(subtraction)){
            System.out.println("The number is " +subtraction);
            elseif(sathya1.nextInt(multiplication)){
                System.out.println("The number is " +multiplication);
                elseif(sathya.1nextInt(division)){
                    System.out.println("The number is " +division);
                }

            }
        }
    }
}

}

线

if(sathya1.nextInt(addition){

没有意义。 这就像说“如果 12”。 其他线路也是如此。 此外,您错过了结束)以及许多其他问题。

也许你的意思是:

import java.util.Scanner;
public class BasicArithmetic
{
    public static void main(String[] args)
    {

    //create a scanner for keyboard input
    Scanner sathya1 = new Scanner(System.in);

    //ask user for the operation to be used
    System.out.print("Please enter the corresponding number to be used \n(1)for Addition,(2)for Subtraction,(3)for Multiplication,(4)for Division:");
    int enteredNumber = sathya1.nextInt();

    //get the two numbers to be used
    System.out.println("Enter the numbers");
    float x = sathya1.nextFloat();
    float y = sathya1.nextFloat();

    //arithmetic operations of the two numbers
    float addition = x+y;
    float subtraction = x-y;
    float multiplication =x*y;
    float division = x/y;

    //if..else statement
    if(enteredNumber==1)
    {
    System.out.println("The sum of the two number is "+addition);
    }
    else if(enteredNumber==2)
    {
    System.out.println("The subtraction of the two number is "+subtraction);
    }
    else if(enteredNumber==3)
    {
    System.out.println("The product of the two number is "+multiplication);
    }
    else if(enteredNumber==4)
    {
    System.out.println("The quotient of the two number is "+division);
    }
    else
    {
     System.out.println("Please enter the correct corresponding number");
    }
    }





}

暂无
暂无

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

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