6.35(幾何:五邊形的面積)五邊形的面積可以使用下面的公式計算:
編寫一個方法,使用下面的方法頭來返回五邊形的面積。
public static double area(double side)
編寫一個主方法股淡,提示用戶輸入五邊形的邊,然后顯示它的面積。
下面是一個運行示例:
Enter the side:5.5
The area of the pentagon is 52.044441
6.35(Geometry: area of a pentagon)The area of a pentagon can be computed using the following formula:
Write a method that returns the area of a pentagon using the following header:
public static double area(double side)
Write a main method that prompts the user to enter the side of a pentagon and displays its area.
Here is a sample run:
Enter the side:5.5
The area of the pentagon is 52.044441
下面是參考答案代碼:
// https://cn.fankuiba.com
import java.util.Scanner;
public class Ans6_35_page205 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the side: ");
double side = input.nextDouble();
System.out.println("The area of the pentagon is "+area(side));
}
public static double area(double side) {
return (5 * side * side) / (4 * Math.tan(Math.PI / 5));
}
}
適用Java語言程序設(shè)計與數(shù)據(jù)結(jié)構(gòu)(基礎(chǔ)篇)(原書第11版)Java語言程序設(shè)計(基礎(chǔ)篇)(原書第10/11版)更多