【问题标题】:I'm trying to call a double variable from one method into another我正在尝试将双变量从一种方法调用到另一种方法
【发布时间】:2014-03-02 11:39:48
【问题描述】:

如何在不在类中创建私有变量的情况下将长度和宽度变量调用到 getArea 方法中,我这样做的方式是导致该方法在它已经运行一次后再次运行。我真的不喜欢这种方式,但教授希望它这样做来模拟“面向对象编程”之前的时代

/*This program allows the user to enter the length and widtch and receive the area
 of the rectangle*/

 import java.util.Scanner;

public class theRectangleCompany
{
  public static void main(String[] args)
  {
    System.out.print("This program will find an area of a Rectangle ");
    getLength();
    getWidth();
    getArea();
  }

  public static double getLength()
  {
    double length;
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Please enter the length ");
    length = keyboard.nextDouble();
    return length;
  }

  public static double  getWidth()
  {
     double width;
     Scanner keyboard = new Scanner(System.in);
     System.out.print("Please enter the width ");
     width = keyboard.nextDouble();
     return width;
  }

  public static void getArea()
    {
      double length = getLength();
      double width = getWidth();
      double area = width * length;
      System.out.println("The area of the Rectangle is: " +area);

    }


}

【问题讨论】:

  • 你说要避免在类中使用私有变量,但是main方法中的变量呢?
  • 斯蒂芬,她只想要主要的方法,而不是我提示用户的消息,
  • 如果方法只能在main中调用,我的答案绝对是解决方案。

标签: java variables methods call


【解决方案1】:

为什么要从 main 方法调用 getLength() 和 getWidth()。只需调用 getArea()

public static void main(String[] args)
  {
    System.out.print("This program will find an area of a Rectangle ");
    getArea();
  }

【讨论】:

  • 教授出于某种原因希望这样做,我同意!如果我这样做,效果会更好
【解决方案2】:

您可以让 getArea 函数接受参数,并使用对其他两个函数的函数调用作为参数:

getArea(getLength(), getWidth());

public static void getArea(double length, double width) { ... }

【讨论】:

    【解决方案3】:

    变化在这里:

    /*This program allows the user to enter the length and widtch and receive the area
     of the rectangle*/
    
     import java.util.Scanner;
    
    public class theRectangleCompany
    {
      public static void main(String[] args)
      {
        System.out.print("This program will find an area of a Rectangle ");    
        getArea();
      }
    
      public static double getLength()
      {
        double length;
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Please enter the length ");
        length = keyboard.nextDouble();
        return length;
      }
    
      public static double  getWidth()
      {
         double width;
         Scanner keyboard = new Scanner(System.in);
         System.out.print("Please enter the width ");
         width = keyboard.nextDouble();
         return width;
      }
    
      public static void getArea()
        {
          double length = getLength();
          double width = getWidth();
          double area = (width * length);
          System.out.println("The area of the Rectangle is: " +area);
    
        }
    
    
    }
    

    【讨论】:

      【解决方案4】:

      不确定这是不是你想要的:

      public static void getArea()
      {
        System.out.println("The area of the Rectangle is: " + (getLength() * getWidth()));
      }
      

      您还需要更改 main 方法以排除 getLength() 和 getWidth():

      public static void main(String[] args)
      {
        System.out.print("This program will find an area of a Rectangle ");
        getArea();
      }
      

      上面的变体类似于

      public static void main(String[] args)
      {
        System.out.print("This program will find an area of a Rectangle ");
        getArea(getLength(),getWidth());
      }
      
      public static void getArea(double length, double width)
      {
        System.out.println("The area of the Rectangle is: " + (length * width));
      }
      

      【讨论】:

        猜你喜欢
        • 2020-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-10
        • 1970-01-01
        • 1970-01-01
        • 2020-08-02
        相关资源
        最近更新 更多