初学者Java:可变范围问题

时间:2014-04-01 00:27:08

标签: java variables methods scope

我正在从我的java书中练习一些工作,我遇到了使用变量进行计算的方法的问题。请注意,这是一项正在进行的工作,我只是想让它使用circleArea方法来计算此刻的圆形区域。这是必要的代码:

public class Geometry    
{   
  public static void printMenu()   
 {    
       System.out.println("This is a geometry calculator\nChoose what you would like  to calculate" + "\n1. Find the area of a circle\n2. Find the area of a     rectangle\n3."   
+ " Find the area of a triangle\n4. Find the circumference of a circle."   
 + "\n5. Find the perimeter of a rectangle\n6. Find the perimeter of a triangle"   
                          + "\nEnter the number of your choice:");   
 }   

   public static void circleArea(double area)   
  {      
    System.out.println(Math.PI*Math.pow(radius, 2));   
  }   

  public static void main(String[] args)   
 {   
  int choice;   //the user's choice    
  double value = 0; //the value returned from the method   
  char letter;  //the Y or N from the user's decision to exit   
  double radius;  //the radius of the circle   
  double length;  //the length of the rectangle   
  double width;  //the width of the rectangle   
  double height;  //the height of the triangle   
  double base;  //the base of the triangle   
  double side1;  //the first side of the triangle   
  double side2;  //the second side of the triangle   
  double side3;  //the third side of the triangle   
   }
}

1 个答案:

答案 0 :(得分:1)

请声明一个类的变量并从中调用该函数。

    public class Geometry    
    {   
int choice;   //the user's choice    
      double value = 0; //the value returned from the method   
      char letter;  //the Y or N from the user's decision to exit   
      double radius;  //the radius of the circle   
      double length;  //the length of the rectangle   
      double width;  //the width of the rectangle   
      double height;  //the height of the triangle   
      double base;  //the base of the triangle   
      double side1;  //the first side of the triangle   
      double side2;  //the second side of the triangle   
      double side3;  //the third side of the triangle 
      public static void printMenu()   
     {    
       System.out.println("This is a geometry calculator\nChoose what you would like to calculate"   
                          + "\n1. Find the area of a circle\n2. Find the area of a     rectangle\n3."   
                          + " Find the area of a triangle\n4. Find the circumference of a circle."   
                          + "\n5. Find the perimeter of a rectangle\n6. Find the perimeter of a triangle"   
                          + "\nEnter the number of your choice:");   
     }   
       public static void circleArea(double area)   
      {      
        System.out.println(Math.PI*Math.pow(radius, 2));   
      }   

      public static void main(String[] args)   
     {   
      Geometry g = new Geometry();
      g.printMenu();
    }
}