【问题标题】:Beginner Java: Variable Scope Issue初学者 Java:变量范围问题
【发布时间】:2014-05-11 13:32:28
【问题描述】:

我正在练习我的 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   
   }
}

【问题讨论】:

  • 您遇到的具体问题是什么?
  • 声明到 main 中的任何变量都不能通过类中的其他函数访问。
  • 家庭作业问题通常在这里做得不好,但您遇到的一个明显问题是您的circleAreaarea 作为参数,而不是作为结果返回它。 public static double circleArea(double radius) 更像是您想要的声明(尽管在您当前的代码中,您只是发出结果而不是返回结果,因此如果您想这样做,您可以保留 void)。
  • 这不是家庭作业,仅供个人练习。使用的变量是本书提供的变量。我想我只是想知道如何让我的方法使用半径变量。

标签: java variables methods scope


【解决方案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();
    }
}

【讨论】:

  • 我想我明白你在做什么,但我不认为这就是我的书想要我做的。我是不是把方法放错地方了?
  • 您在一个函数中声明了变量,并试图从另一个函数访问。简单来说,在一个函数中声明的任何变量都不能在另一个函数中访问。
猜你喜欢
  • 2021-01-28
  • 1970-01-01
  • 1970-01-01
  • 2013-02-08
  • 2019-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-15
相关资源
最近更新 更多