Java初学者:可变范围问题 [英] Beginner Java: Variable Scope Issue

查看:61
本文介绍了Java初学者:可变范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在练习Java书籍中的一些工作,但是在获取使用变量进行计算的方法时遇到问题.请注意,这是一个正在进行中的工作,我现在只是想让它使用circleArea方法来计算圆形的面积.这是必要的代码:

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

推荐答案

请声明class变量并从中调用函数.

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

这篇关于Java初学者:可变范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆