Java程序周长和区域

时间:2015-07-31 14:23:30

标签: java variable-declaration

我得到了编写Java程序的任务,该程序给出了半径为1到50的圆的圆周和面积。这就是我所拥有的:

public class Circles {

public static void main(String[] args) {


}   
{
    for (int i = 1; i <=50; i=i+1)
     area = PI * (radius * radius);
      System.out.println("The area is " + area);
      double circumference= PI * 2*radius;
      System.out.println( "The circumference "+circumference) ;
   }


}

它表示无法将radius,pi和area解析为变量。

3 个答案:

答案 0 :(得分:0)

因为你从未将它们声明为变量(在PIradius的情况下,甚至也没有给它们赋值)。请注意,它抱怨您 宣布的circumference

答案 1 :(得分:0)

您必须声明您的变量 - 此时您只需声明其中两个变量,即icircumference。此外,Java有一个PI常量,可以在Math类中找到,因此您需要Math.PI

public static void main(String[] args) {
    for (int i = 1; i <=50; i=i+1) {
         double radius = i;
         double area = Math.PI * (radius * radius);
         System.out.println("The area is " + area);
         double circumference = Math.PI * 2*radius;
         System.out.println( "The circumference "+circumference) ;
    }
}

答案 2 :(得分:0)

public class Circles {

    public static void main(String[] args) {

        double PI = Math.PI;

        for (int radius = 1; radius <=50; i=i+1) {
           double area = PI * (radius * radius);
           System.out.println("The area is " + area);
           double circumference= PI * 2*radius;
           System.out.println( "The circumference "+circumference) ;
       }

    }