尝试运行我的上一个方法时出现错误

时间:2019-04-09 05:33:37

标签: java methods

我的程序要求我创建4种方法。 1表示长度,1表示宽度,1表示面积,1表示面积。我的代码似乎正在工作,直到需要显示我的区域的最终方法为止。我已经尝试了几乎所有我能想到的一切,但仍然无法正常工作。

    import java.io.*;
    import java.util.*;

    public class Lab9Q2
    {
      public static double getLength()
    {
        Scanner keyboard = new Scanner (System.in);          // Create Method
        System.out.println ("Enter the length of the rectange"); // ask for the length 
        double length = keyboard.nextDouble();
        return length;
      }

      public static double getWidth()
      {
        Scanner keyboard = new Scanner (System.in);          // Create Method
        System.out.println ("Enter the width of the rectange"); // ask for the width
        double width = keyboard.nextDouble();
        return width;
      }

      public static double getArea (double length, double width)
      {
        double area; 
        area = length*width;
        return area; 
      }
      public static double displayArea (double length, double width, double area)
      {
        System.out.println ("The length is: " + length);
        System.out.println ("The width is: " + width);
        System.out.println ("The area of the rectangle is: " + area);
      }
    public static void main (String [] args)
    {
      getLength();
      getWidth();
      displayArea(length, width, area);
    }
    }

程序应使用我的所有方法调用,然后正确显示结果,但不会这样做。

3 个答案:

答案 0 :(得分:1)

您可能打算在对displayArea()的最终调用中使用helper方法的三个结果:

public static void main (String[] args) {
    double length = getLength();
    double width = getWidth();
    double area = getArea(length, width);
    displayArea(length, width, area);
}

答案 1 :(得分:1)

按如下所示更改main

public static void main(String [] args) {
  double length = getLength();
  double width = getWidth();
  double area  = getArea(length, width);
  displayArea(length, width, area);
}

您错过了分配并调用了getArea函数

答案 2 :(得分:0)

两种使代码正常工作的方法

1)将主要方法更改为

public static void main (String[] args) {
double length = getLength();
double width = getWidth();
double area = getArea(length, width);
displayArea(length, width, area);
}

2)全局声明您的长度,宽度,面积。

import java.io.*;
import java.util.*;

public class Lab9Q2
{
  public static double length;
  public static double width;
  public static double area;

  public static void getLength()
{
    Scanner keyboard = new Scanner (System.in);          // Create Method
    System.out.println ("Enter the length of the rectange"); // ask for the length 
    length = keyboard.nextDouble();

  }

  public static void getWidth()
  {
    Scanner keyboard = new Scanner (System.in);          // Create Method
    System.out.println ("Enter the width of the rectange"); // ask for the width
    width = keyboard.nextDouble();

  }

  public static void getArea (double length, double width)
  {
    area = length*width;

  }
  public static double displayArea (double length, double width, double area)
  {
    System.out.println ("The length is: " + length);
    System.out.println ("The width is: " + width);
    System.out.println ("The area of the rectangle is: " + area);
  }
public static void main (String [] args)
{
  getLength();  //Here length will get initialised
  getWidth();   //Here width will get initialised
  getArea();   //Here area will get calculated ..you also missed this statement
  displayArea(length, width, area);
}
}