Получение ошибки при попытке запустить мой последний метод - PullRequest
0 голосов
/ 09 апреля 2019

Моя программа требует, чтобы я создал 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 ]

1 голос
/ 09 апреля 2019

Измените блок main, как показано ниже

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

Вы пропустили назначения и вызвали getArea функцию

1 голос
/ 09 апреля 2019

Два способа заставить ваш код работать

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);
}
}
1 голос
/ 09 апреля 2019

Вы, вероятно, намеревались использовать три результата из вспомогательных методов в последнем вызове displayArea():

public static void main (String[] args) {
    double length = getLength();
    double width = getWidth();
    double area = getArea(length, width);
    displayArea(length, width, area);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...