方法和返回问题

时间:2016-03-24 22:58:08

标签: java methods area

任何人都知道此错误的含义:

RectangleArea.java:21: error: method getLength in class RectangleArea cannot be applied to given types;


  length = getLength();
            ^
  required: double,Scanner

  found: no arguments

  reason: actual and formal argument lists differ in length 

     import java.util.Scanner;

     public class RectangleArea
     {
       public static void main (String [] args)
       {
         Scanner keyboard = new Scanner (System.in);
         double length,    // The rectangle's length
         width,     // The rectangle's width\
         area;      // The rectangle's area
         welcomeBanner ();
         // Get the rectangle's length from the user.
         length = getLength();
      }
      static void getLength(double aLength, Scanner aKeyboard)
      {
        System.out.print("Enter the rectangle's length: ");
        aLength = aKeyboard.nextDouble ();
        System.out.println("");
        return aLength;
      }
    }

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

getLength是一种无效方法。如果您想要返回一些内容,则需要将void替换为数据类型double。您还需要确保传递正确的参数类型和正确数量的参数。

答案 1 :(得分:0)

方法getLength()

  1. 需要2个参数double aLengthScanner aKeyboard
  2. 您要将此值指定为double的长度,但您不会从方法void返回任何内容(getLength())。
  3. 如果您尝试这样做怎么办:

    static double getLength()
    {
        Scanner keyboard = new Scanner (System.in);
        double aLength;
        System.out.print("Enter the rectangle's length: ");
        length = keyboard.nextDouble ();
        System.out.println("");
        return length;
    }
    

    此外,您应该从keyboard方法中删除变量main()