无法想出这段代码。请回馈一些见解

时间:2015-02-19 19:38:30

标签: java methods

// This application displays some math facts
public class DebugThree2 {

    public static void main(String args[]) {
      add();
      subtract();
      System.out.println("Java");
      int a = 2, b = 5, c = 10;
      add("a" + "b");
      add("b" + "c");
      subtract("c" - "a");            
    }

    public static void add() {
      System.out.println("The sum of " + "a" + "and" + "b" + "is" + "a" + "b");
    }

    public static void subtract() {
      System.out.println("The difference between " + "a" + "and" + "b" + "is" + "a" - "b");
    }
} 

我不断收到错误method add in class DebugThree2 cannot be applied to given typesbad operand types for binary operator '-'

1 个答案:

答案 0 :(得分:0)

public static void main(String args[])
{
  add();
  subtract();
  System.out.println("Java");
  int a = 2, b = 5, c = 10;
  add(a,b);
  add(b,c);
  subtract(c,a);            
}
public static void add(int a, int b)
{
  System.out.println("The sum of " + a +
     " and " +  b  + " is " + (a+b));
}
public static void subtract(int a, int b)
{
  System.out.println("The difference between " +
    a + " and  " + b + " is " + (a-b));
}

我觉得这样的事情可能更符合您的需求。您试图从(字符串)a中减去(字符串)b。

"a" - "b"

您必须使用其实际值,不包括引号。