Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java I';我试图将一个双变量从一个方法调用到另一个方法_Java_Variables_Methods_Call - Fatal编程技术网

Java I';我试图将一个双变量从一个方法调用到另一个方法

Java I';我试图将一个双变量从一个方法调用到另一个方法,java,variables,methods,call,Java,Variables,Methods,Call,如何在不在类中创建私有变量的情况下将length和width变量调用到getArea方法中,我这样做的方式是使该方法在已经运行一次之后再次运行。我真的不喜欢这种方式,但教授希望用这种方式来模拟“面向对象编程”之前的时代 为什么要从main方法调用getLength()和getWidth()。只需调用getArea() 为什么要从main方法调用getLength()和getWidth()。只需调用getArea() 您可以让getArea函数获取参数,并使用对其他两个函数的函数调用作为参数: g

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


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


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


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

getArea(getLength(), getWidth());

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

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

getArea(getLength(), getWidth());

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

此处有更改:

/*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);

    }


}

此处有更改:

/*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);

    }


}

不确定这是否是您想要的:

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

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

不确定这是否是您想要的:

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

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

你说你想避免在类中使用私有变量,但是在主方法中使用变量呢?stephen,她只需要在主方法中使用方法,而不是我提示用户的消息,如果只需要在主方法中调用方法,我的答案肯定是解决方案。你说你想避免在类中使用私有变量,但是main方法中的变量呢?stephen,她只需要main方法,而不是我提示用户的消息,如果只需要在main中调用方法,我的答案肯定是解决方案。教授出于某种原因希望这样做,我同意!如果我那样做效果会更好这是教授出于某种原因想要的方法,我同意!如果我那样做,效果会更好