Ⅰ java編程求矩形的面積
代碼如下:
import java.util.*;
public class Rectangle {
private float length; //定義長變數
private float width; // 寬變數
public Rectangle(float length,float width){
this.length=length;
this.width=width;
}
public float getGirth(){
return (length+width)*2;
} //求周長方法
public float getArea(){
return length*width;
} //求面積方法
public static void main (String[] args) {
Scanner in=new Scanner(System.in);//調用輸入方法
System.out.println ("請輸入矩形的長:");
float a=in.nextFloat();
System.out.println ("請輸入矩形的寬:");
float b=in.nextFloat();
System.out.println ("矩形周長為:"+new Rectangle(a,b).getGirth());
System.out.println ("矩形面積為:"+new Rectangle(a,b).getArea());
}
}
Ⅱ 如何用java定義方法求圓的面積
定義一個襲方法,參數為半徑r,返回面積。代碼如下:
publicclassTest(){
publicdoublegetArea(doubler){
doublearea=Math.PI*r*r;
returnarea;
}
publicstaticvoidmain(String[]args){
Testt=newTest();
Scannerscanner=newScanner(System.in);
System.out.print("請輸入半徑:");
Stringstr=scanner.nextLine();
doubler=Double.parseDouble(str);//將輸入的半徑轉為double類型
doublearea=t.getArea(r);
System.out.print("圓的面積為:"+area);
}
}
Ⅲ JAVA編程中求圓的面積怎麼寫
import java.util.Scanner;
//java 並不直接支持控制台輸入,但可以使用Scanner類建立它的對象,已讀取來System.in的輸入
public class KongZhi {
public static void main(String[] args) {
Scanner input = new Scanner( System.in);
System.out.println("Enter a number for radius: ");
double radius = input.nextDouble();
double area= radius*radius*3.14;
System.out.println("The area for the circle of radius "+radius+" "+area);
}
}
(3)java求面積擴展閱讀:
其他方法求圓的面積:
#include<stdio.h>
#include<math.h>
#define M=3.14
int main()
{
int r;
double s;
scanf("%d",&r);
s=M*pow(r,2);
printf("%0.7lf",s);
return 0;
}