我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!
天气: 晴朗
心情: 高兴
/*fileName:P5Q16.java
*Name:Wang Chaoran
* Description:
16 (The MyTriangle class)
Create a class named MyTriangle that contains the following two methods:
//Returns true if the sum of any two sides is greater than the third side.//
public static boolean isValid(double side1, double side2, double side3)
// Returns the area of the triangle.//
public static double area(double side1, double side2, double side3)
The formula for computing the area is
Write a test program that reads three sides for a triangle and computes
the area if the input is valid. Otherwise, it displays that the input is invalid.
*/
import java.util.Scanner;
public class P5Q16{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Please enter side1: ");
double side1 = scan.nextDouble();
System.out.println("Please enter side2: ");
double side2 = scan.nextDouble();
System.out.println("Please enter side3: ");
double side3 = scan.nextDouble();
if(isValid(side1,side2,side3))
System.out.println("The area of the triangle is "+area(side1,side2,side3));
else
System.out.println("The input is invalid.");
}
public static boolean isValid(double side1, double side2, double side3){
if((side1+side2)>side3)
if((side1+side3)>side2)
if((side2+side3)>side1)
return true;
return false;
}
public static double area(double side1, double side2, double side3){
double s = (side1 + side2 + side3)/2;
return Math.sqrt(s*(s-side1)*(s-side2)*(s-side3));
}
}
导入论坛查看(36)回复(0)引用(0)好评(0) 差评(0)
加入收藏
编辑
审核
TAG:
computing