JAVA求圓的面積

來源:互聯網
上載者:User

標籤:system   輸出   面積   int   輸入   scan   print   str   保留   

import java.text.DecimalFormat;
import java.util.Scanner;

public class TheAreaOfCircle {
public static void main(String[] args) {
/*問題描述
給定圓的半徑r,求圓的面積。
輸入格式
輸入包含一個整數r,表示圓的半徑。
輸出格式
輸出一行,包含一個實數,四捨五入保留小數點後7位,表示圓的面積。
說明:在本題中,輸入是一個整數,但是輸出是一個實數。

對於實數輸出的問題,請一定看清楚實數輸出的要求,比如本題中要求保留小數點後7位,則你的程式必須嚴格的輸出7位小數,輸出過多或者過少的小數位元都是不行的,都會被認為錯誤。

實數輸出的問題如果沒有特別說明,舍入都是按四捨五入進行。

範例輸入
4
範例輸出
50.2654825
資料規模與約定
1 <= r <= 10000。*/
Scanner sc=new Scanner(System.in);
int r=sc.nextInt();;
while(r<1|r>10000){
r=sc.nextInt();
}
DecimalFormat df =new DecimalFormat("#####0.0000000");

double circle=r*r*Math.PI;
//double b=Math.round(circle*10000000)/10000000.0000000;
//System.out.println(b);
System.out.println(df.format(circle));
}

}

JAVA求圓的面積

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.