# Java 实例 计算圆的面积和周长

1）用户交互：程序将提示用户输入圆的半径

2）没有用户交互：半径值将在程序本身中指定。

/**
* @author: BeginnersBook.com
* @description: Program to calculate area and circumference of circle
* with user interaction. User will be prompt to enter the radius and
* the result will be calculated based on the provided radius value.
*/
import java.util.Scanner;
class CircleDemo
{
static Scanner sc = new Scanner(System.in);
public static void main(String args[])
{
/*We are storing the entered radius in double
* because a user can enter radius in decimals
*/
System.out.println("The area of circle is: " + area);
System.out.println( "The circumference of the circle is:"+circumference) ;
}
}


Enter the radius: 1
The area of circle is: 3.141592653589793
The circumference of the circle is:6.283185307179586


/**
* @author: BeginnersBook.com
* @description: Program to calculate area and circumference of circle
* without user interaction. You need to specify the radius value in
* program itself.
*/
class CircleDemo2
{
public static void main(String args[])
{
System.out.println("The area of circle is: " + area);
System.out.println( "The circumference of the circle is:"+circumference) ;
}
}


The area of circle is: 28.274333882308138
The circumference of the circle is:18.84955592153876


• 回顶