【问题标题】:error: bad operand types for binary operator '-'错误:二元运算符“-”的操作数类型错误
【发布时间】:2015-08-29 10:34:17
【问题描述】:

这是我的源代码:

/* 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 新手,但我仍在研究算术。我以为我做得很好,但我不明白出了什么问题。这很可能是一个真正的菜鸟错误,但你能告诉我我做错了什么吗?

【问题讨论】:

    标签: java math


    【解决方案1】:
    "a - b is " + (a) - (b)`
    

    意思是:

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

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

    你需要使用括号:

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

    【讨论】:

    • 谢谢!我只是把脸拍得太厉害了。
    【解决方案2】:

    +- 被视为字符串操作,其中表达式从左到右进行计算。为了避免这种情况标记,减法具有高优先级,当您用 () 包围时会发生这种情况

        System.out.println( "a - b is " + (a - b) );
    

    现在由于优先级,括号(a-b) 内的表达式首先求值,然后是从左到右的整个表达式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-02
      • 1970-01-01
      • 2016-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多