在Java中的toString方法中返回值的问题

时间:2019-01-11 00:57:54

标签: java

我试图在我的toString方法中返回int值a,b和c,但是尽管有一个“;”,但它却显示了“;预期”。我不明白怎么了。我已经尝试过类似“ APLine.getA()”和“ getA()”的方法,但是它们都没有起作用。请帮我。我感到很难过。

public class APLine
{
 private int a;
 private int b;
 private int c;
 private int x;
 private int y;

public APLine()
{
 a = 0;
 b = 0;
 c = 0;
 x = 0;
 y = 0;
} 

 public APLine(int A, int B, int C)
{
 a = A;
 b = B;
 c = C;
}

 public void setA(int A)
{
 a = A;
}
 public void setB(int B)
{
 b = B;
}
 public void setC(int C)
{
 c = C;
}

public int getA()
{
 if(a != 0)
 {
 return a;
 }
}

public int getB()
{
 if(b != 0)
 {
 return b;
 }
}

public int getC()
{
 return c;
}

public double getSlope()
{
 return (double)(-a)/b;
}

public boolean isOnLine(int x, int y)
{
 if(a*x + b*y + c == 0)
 {
   return true;
 }
  else
 {
  return false;
 }
}

public String toString()
{
 return ""a,b,c;
}

}

这是主要方法。

class Main {
public static void main(String[] args) {
APLine myLine = new APLine(5, 4, -17);


System.out.println("The slope is: "+ myLine.getSlope());
System.out.println("Is (5, 2) on the the line? "+ myLine.isOnLine(5,2));
System.out.println("Is (1, 4) on the the line? "+ myLine.isOnLine(1,4));

myLine = new APLine(-25, 40, 30);


System.out.println("The slope is: "+ myLine.getSlope());
System.out.println("Is (5, -2) on the the line? "+ myLine.isOnLine(5,-2));
System.out.println("Is (6, 3) on the the line? "+ myLine.isOnLine(6,3));
  }
}

这就是编译器显示的内容。

APLine.java:78: error: ';' expected
 return ""a,b,c;
          ^
APLine.java:78: error: ';' expected
 return ""a,b,c;
           ^
APLine.java:78: error: not a statement
 return ""a,b,c;
            ^
APLine.java:78: error: ';' expected
 return ""a,b,c;
             ^
APLine.java:78: error: not a statement
 return ""a,b,c;
              ^
5 errors

1 个答案:

答案 0 :(得分:0)

更改

public String toString(){return ""a,b,c;}

public String toString(){return String.format("%s,%s,%s", a,b,c);}

您的表达式无效。