簡體   English   中英

初學者 Java:變量范圍問題

[英]Beginner Java: Variable Scope Issue

我正在練習我的 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   
   }
}

請聲明一個類的變量並從中調用函數。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM