用Java编写一个小程序:要求从键盘输入圆的半径,求出圆的面积
创新互联公司是一家专注于成都网站建设、成都网站制作与策划设计,黎城网站建设哪家好?创新互联公司做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:黎城等地区。黎城做网站价格咨询:18982081108
package YongXingJava1;
import java.io.*;
public class demol1{
public static void main(String[] args) {
try{
//输入流,从键盘接收数
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
System.out.println("请输入圆的半径:");
//从控制台读取一行数据
String a1=br.readLine();
//把string转换成float
float num1=Float.parseFloat(a1);
float result=0;
float p=(float)3.14;
result=p*num1*num1;
if(num1=0)
{
System.out.println("圆的半径必须大于零");
}
if(num10)
{
System.out.println("圆的面积为"+result);
}
} catch(Exception e)
{
e.printStackTrace();
}
}
}
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);
}
}
扩展资料:
其他方法求圆的面积:
#includestdio.h
#includemath.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;
}
public class Exam
{
public static void main(String[] args)
{
Circle c=new Circle(3,4,5);
System.out.printf("圆心:(%f,%f),半径:%f,面积:%f",c.x,c.y,c.r,c.countArea());
}
}
class Circle
{
public Circle()
{
this(0,0,0);
}
public Circle(double x,double y,double r)
{
this.x=x;
this.y=y;
this.r=r;
}
public double countArea()
{
return Math.PI*r*r;
}
/*private*/public double x,y,r;
}
代码如下:
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.JFrame; import javax.swing.JPanel; public class TestSw extends JFrame { public static void main(String[] args) { new TestSw(); } public TestSw(){ super("Test"); this.setSize(new Dimension(400,300)); this.setContentPane(new Mypane()); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } class Mypane extends JPanel{ public void paint(Graphics g) { super.paint(g); g.setColor(Color.red); g.setXORMode(Color.white); g.drawArc(20, 20, 100, 100, 0, 360); ///此方法将画一个直径100的圆.红色. } } }