-1
當我運行我的程序而不是從我的RightTriangle類中的toString方法接收字符串時,我應該正常收到字符串的內存位置,而我從驅動程序中打印對象時。我該如何解決這個問題?reciving memory location而不是字符串輸出
這是RightTriangleDriver類
import java.util.*;
public class RightTriangleDriver
{
public static void main (String [] args)
{
Scanner reader = new Scanner (System.in);
System.out.println ("Enter the length of the first leg ");
double leg1 = reader.nextDouble();
System.out.println ("Enter the length of the second leg ");
double leg2 = reader.nextDouble();
RightTriangle f1 = new RightTriangle (leg1, leg2);
System.out.println (f1);
}
}
這是直角三角形類。
public class RightTriangle
{
private double leg1;
private double leg2;
private double hyp;
public RightTriangle (double one , double two)
{
leg1 = one;
leg2 = two;
hyp = Math.sqrt (Math.pow (leg1 , 2) + Math.pow (leg2 , 2));
}
public double perimiter()
{
double perimiter = 0;
perimiter = leg1 + leg2 + hyp;
return perimiter;
}
public double area()
{
double area = 0;
area = (leg1*leg2)/2;
return area;
}
public String toSting()
{
String str;
str = "Leg 1 is " + leg1 + " units long. Leg 2 is " + leg2 + " units long." + "\n" + "the Hypotenuse is " + hyp +
" units long." + "\n" + " The perimiter is " + perimiter() + " units and the area is " + area() +
" units squared.";
return str;
}
}
拼寫錯誤'toSting' - >'toString' – talex
fyi,它不是你所看到的內存位置,那是一個散列。有關更多信息,請參見[this](http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#toString())。 –