Beginner Java: Variable Scope Issue

纵然是瞬间 提交于 2020-01-22 03:48:09

问题


I'm practicing some work from my java book and I'm having an issue with getting a method to use a variable for a calculation. Please note that this is a work in progress and I'm only trying to get it to use the circleArea method to calculate the area of a circle at the moment. Here is the necessary code:

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:


Please declare a variable of class and call the function from it.

    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();
    }
}


来源:https://stackoverflow.com/questions/22774655/beginner-java-variable-scope-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!