0
你好,我試圖建立一個基本的程序找到一個三角形的面積和周長。我對日食很新。編譯器不會爲該程序運行或顯示錯誤,但會運行先前運行的程序。我的程序的代碼是:甚至沒有錯誤,也沒有在日食中運行的程序
import java.util.Scanner;
public class Solution
{
public static void main()
{
Scanner input=new Scanner(System.in);
double b,h,o,t;
System.out.println("Enter the length of base");
b=input.nextDouble();
System.out.println("Enter the length of heigth");
h=input.nextDouble();
System.out.println("Enter the length of sideOne");
o=input.nextDouble();
System.out.println("Enter the length of sideTwo");
t=input.nextDouble();
input.close();
Attributes Val= new Attributes();
Val.setbase(b);
Val.setheight(h);
Val.setsideOne(o);
Val.setsideTwo(t);
double result=Val.area();
System.out.println("the area of triangle is:"+result);
result=Val.peri();
System.out.println("the perimeter of triangle is:"+result);
}
}
另一類是
public class Attributes
{
private double base,height,sideOne,sideTwo;
public double area()
{
double area=this.base*this.height/2;
return area;
}
public double peri()
{
double peri=base+sideOne+sideTwo;
return peri;
}
public double getbase()
{
return this.base;
}
public double getheight()
{
return this.height;
}
public double getsideOne()
{
return this.sideOne;
}
public double getsideTwo()
{
return this.sideTwo;
}
public void setbase(double base)
{
this.base=base;
}
public void setheight(double height)
{
this.height=height;
}
public void setsideOne(double sideOne)
{
this.sideOne=sideOne;
}
public void setsideTwo(double sideTwo)
{
this.sideTwo=sideTwo;
}
}
你能幫助我的問題,並建議我,如果任何錯誤出現在節目。 在此先感謝。 :)
對代碼沒有真正的評論,但是如果你採用三邊的長度,你不需要高度來找到該區域。 [蒼鷺的公式](http://www.mathopenref.com/heronsformula.html) – Holloway 2014-10-03 13:26:45
好吧,我知道這個公式。但是我給了具體的變量和方法名稱來練習。我是一名初學者,我正在學習編碼,所以我遵循該問題陳述,然後編寫了我的代碼。甚至其他問題也會出現,如雙方總和概念必須大於其他方面,但是當我學習如何編碼時,這些對我來說是最不重要的。而且,我必須改善向代碼添加註釋的習慣。感謝您的建議。 – 2014-10-03 13:44:11