错误:二进制运算符“-"的操作数类型错误 [英] error: bad operand types for binary operator '-'

查看:282
本文介绍了错误:二进制运算符“-"的操作数类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的源代码:

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
    System.out.println( "Integer.MAX_VALUE = " +   Integer.MAX_VALUE );
    System.out.println( "Integer.MAX_VALUE + 1 = " +   (Integer.MAX_VALUE + 1) );
    System.out.println( "Integer.MAX_VALUE * 2 = " +  ( Integer.MAX_VALUE * 2) );
    System.out.println( "Integer.MAX_VALUE * 5 = " +  ( Integer.MAX_VALUE * 5) );
    System.out.println( "Integer.MAX_VALUE * 10 = " + (  Integer.MAX_VALUE * 10) );
    System.out.println( "Integer.MAX_VALUE = " +   Integer.MAX_VALUE );
    System.out.println( "Integer.MIN_VALUE - 1 = " +   (Integer.MIN_VALUE - 1) );
    System.out.println( "Integer.MIN_VALUE * 2 = " +  ( Integer.MIN_VALUE * 2) );
    System.out.println( "Integer.MIN_VALUE * 5 = " +  ( Integer.MIN_VALUE * 5) );
    System.out.println( "Integer.MIN_VALUE * 10 = " + (  Integer.MIN_VALUE * 10) );
    //Part 2
    System.out.println( "Integer.MAX_VALUE + 1.0 = " +   (Integer.MAX_VALUE + 1.0) );
    System.out.println( "Integer.MAX_VALUE * 2.0 = " +  ( Integer.MAX_VALUE * 2.0) );
    System.out.println( "Integer.MAX_VALUE * 5.0 = " +  ( Integer.MAX_VALUE * 5.0) );
    System.out.println( "Integer.MAX_VALUE * 10.0 = " + (  Integer.MAX_VALUE * 10.0) );
    System.out.println( "Integer.MIN_VALUE - 1.0 = " +   (Integer.MIN_VALUE - 1.0) );
    System.out.println( "Integer.MIN_VALUE * 2.0 = " +  ( Integer.MIN_VALUE * 2.0) );
    System.out.println( "Integer.MIN_VALUE * 5.0 = " +  ( Integer.MIN_VALUE * 5.0) );
    System.out.println( "Integer.MIN_VALUE * 10.0 = " + (  Integer.MIN_VALUE * 10.0) );
    //Part 3
    int a, b;
    a = 1;
    b = 2;
    System.out.println( "The ints a, b are " + a + ", " + b );
    System.out.println( "a + b is " + a + b );
    System.out.println( "a - b is " + a - b );
    System.out.println( "a * b is " + a * b );
    System.out.println( "a / b is " + a / b );
    //Part 4
    double aD, bD;
    aD = 1.0;
    bD = 2.0;
    System.out.println( "The doubles aD, bD are " + aD + ", " + bD  );
    System.out.println( "aD + bD is " + aD + bD );
    System.out.println( "aD - bD is "  + aD - bD );
    System.out.println( "aD * bD is " + aD * bD );
    System.out.println( "aD / bD is " + aD / bD );
}
}

这是我的错误:

Compilation error   time: 0.1 memory: 320512 signal:0
Main.java:37: error: bad operand types for binary operator '-'
    System.out.println( "a - b is " + a - b );
                                        ^
 first type:  String
 second type: int
Main.java:46: error: bad operand types for binary operator '-'
    System.out.println( "aD - bD is "  +aD - bD );
                                           ^
first type:  String
second type: double
2 errors

我是Java的新手,但我仍在弄清楚算术.我以为我还不错,但是我不明白出了什么问题.这很可能是一个真正的菜鸟错误,但是您能告诉我我做错了什么吗?

I am new to java and I am still figuring out arithmetic. I thought I was doing fine but I don't understand what's gone wrong. It is most probably a real rookie mistake but could you tell me what I did wrong?

推荐答案

"a - b is " + (a) - (b)`

表示:

("a - b is " + (a)) - (b)

.左侧(("a - b is " + (a)))是一个字符串,您不能从字符串中减去.

. The left part (("a - b is " + (a))) is a string, and you can't subtract from a string.

您需要使用括号:

"a - b is " + (a - b)

这篇关于错误:二进制运算符“-"的操作数类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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