2014-04-01 72 views
-2

我正在從我的java書中練習一些工作,我遇到了使用變量進行計算的方法的問題。請注意,這是一項正在進行的工作,我只是試圖讓它使用circleArea方法來計算此時的圓的面積。這裏是必要的代碼:初學者Java:變量作用域問題

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 
    } 
} 
+0

究竟是什麼問題? –

+0

聲明爲main的任何變量不能在類中的其他函數中訪問。 – kinkajou

+2

家庭作業問題在這裏通常不會很好,但是一個明顯的問題是您的'circleArea'將'area'作爲參數,而不是將其作爲結果返回。 'public static double circleArea(double radius)'會更像你想要的聲明(儘管在你當前的代碼中你只是發出結果而不是返回它,所以如果你想要這樣做,你會保留'void'那樣)。 –

回答

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

我想我明白你在做什麼,但我不認爲這是我的書如何讓我做到這一點。我把方法放在錯誤的地方嗎? – user3483088

+0

您在一個函數中聲明瞭變量,並試圖從另一個函數進行訪問。只需在一個函數中聲明的任何變量都不能在另一個函數中訪問。 – kinkajou