2014-10-03 53 views
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; 
    } 
} 

你能幫助我的問題,並建議我,如果任何錯誤出現在節目。 在此先感謝。 :)

+0

對代碼沒有真正的評論,但是如果你採用三邊的長度,你不需要高度來找到該區域。 [蒼鷺的公式](http://www.mathopenref.com/heronsformula.html) – Holloway 2014-10-03 13:26:45

+0

好吧,我知道這個公式。但是我給了具體的變量和方法名稱來練習。我是一名初學者,我正在學習編碼,所以我遵循該問題陳述,然後編寫了我的代碼。甚至其他問題也會出現,如雙方總和概念必須大於其他方面,但是當我學習如何編碼時,這些對我來說是最不重要的。而且,我必須改善向代碼添加註釋的習慣。感謝您的建議。 – 2014-10-03 13:44:11

回答

2

您尚未正確聲明main方法。由於程序的執行從main方法開始,請確保您正在運行包含正確聲明main方法的類。

你的主要方法應該是這樣的:

public static void main(String args[]) { 
    // Starting point of application 
} 

更多信息請參見例如在wiki at c2.com

+1

老兄非常感謝,我從一個小時起就在撓頭。現在是我瞭解基礎知識的時候了。 – 2014-10-03 13:18:02

相關問題