繁体   English   中英

java中toString方法返回值的问题

[英]Problem with returning values in toString method in 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

改变

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

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

你的表达无效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM