java中toString方法中返回值的问题

java中toString方法中返回值的问题,java,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

我试图在我的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
改变


您的表达式无效。

请尝试
“”+a+b+c。但是,这将返回类似于
234
的内容。所以可能
返回“a=“+a+”\nb=“+b+”\nc=“+c
更像您想要的东西您在
toString()
方法中尝试了什么?您也可以尝试使用
String.join
静态方法。一个示例用法是:
返回String.join(“,”,a,b,c)
String.join()
将不起作用@prasadHow
public int getA(){if(a!=0){return a;}}
甚至编译?在所有情况下都没有返回值
public String toString(){return ""a,b,c;}
public String toString(){return String.format("%s,%s,%s", a,b,c);}