2014-10-03 91 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

相关问题