java记——函数调用,三角形问题

问题描述:

输入三角形的三边,判断是否能成组成三角形,如果可以,则输出三角形的面积,如果可以组成三角形,则报错

程序代码:

import java.text.DecimalFormat;
import java.util.Scanner;
public class Triangle {
public static void main(String[] args){
	@SuppressWarnings("resource")
	Scanner input=new Scanner(System.in);
	System.out.println("Please enter the side of Triangle: ");
	double side1=input.nextDouble();
	double side2=input.nextDouble();
	double side3=input.nextDouble();
	if(isTriangle(side1,side2,side3)){
		DecimalFormat df=new DecimalFormat("0.000");
		double S=area(side1,side2,side3);
		System.out.println("The area of Triangle is: "+df.format(S));
	}
	else
	System.out.println("The threes of triangle is wrong !!");
}
public static boolean isTriangle(double side1,double side2,double side3){
	if((side1+side2>side3)&&(side1+side3>side2)&&(side2+side3>side1)){
		return true;
	}
	else
		return false;	
}
public static double area(double side1,double side2,double side3){
	double p=(side1+side2+side3)/2;
	return Math.sqrt(p*(p-side1)*(p-side2)*(p-side3));
}
}
运行结果:



你可能感兴趣的:(java记——函数调用,三角形问题)