日期:2014-05-20  浏览次数:20715 次

求解问题!
public class Triangle {
  public static void main(String [] args){
  TriangleCharacter tc1;
  tc1=new TriangleCharacter();
  tc1.setLine(3,4,5);
  System.out.println(tc1.getLine_1()+","+tc1.getLine_2()+","+tc1.getLine_3());
  System.out.println(tc1.getPerimeter());
  System.out.println(tc1.getArea());
  }
}
class TriangleCharacter{

private float line_1,line_2,line_3;
private float area, pm;
public void setLine(float l1,float l2,float l3){
line_1=l1;
line_2=l2;
line_3=l3;
float temp=0;
if(line_1<line_2)
  {temp=line_1;line_2=line_1;line_1=temp;}
  if(line_1<line_3)
  {temp=line_1;line_1=line_3;line_3=temp;}
  if(line_2<line_3)
  {temp=line_2;line_2=line_3;line_3=temp;}
if(line_3+line_2<line_1){
System.out.println("This is not a triangle");
line_1=line_2=line_3=0;
}

}
public float getLine_1(){

return line_1;
}
public float getLine_2(){

return line_2;
}
public float getLine_3(){

return line_3;
}
public float getPerimeter(){
pm = line_1+line_2+line_3;
return (pm);
}
  public float getArea(){
  area=(float)Math.sqrt(this.getPerimeter()*(this.getPerimeter()/2.0f-line_1)*(this.getPerimeter()/2.0f-line_2)*(this.getPerimeter()/2.0f-line_3));
  return area;
  }
}
本程序中我在setLine()里添加的值是3,4,5
可是为什么显示的数字是5,3,3?而不是5,4,3?输入别的数据也是一样显示5,3,3
求解...

------解决方案--------------------
错误: {temp=line_1;line_2=line_1;line_1=temp;}
正确: {temp=line_2;line_2=line_1;line_1=temp;}
------解决方案--------------------
探讨

错误: {temp=line_1;line_2=line_1;line_1=temp;}
正确: {temp=line_2;line_2=line_1;line_1=temp;}