Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.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 方法和返回问题_Java_Methods_Area - Fatal编程技术网

Java 方法和返回问题

Java 方法和返回问题,java,methods,area,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 lis

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

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;
      }
    }

如何修复此问题?

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

方法
getLength()

  • 它需要两个参数
    double-aLength
    Scanner-aKeyboard
  • 您正在将此值指定给双精度
    的length,但是您没有从方法
    getLength()
    返回任何内容(
    void
  • 如果您尝试以下方法:

    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;
    }
    

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

    请查看
    getLength
    的参数。。。然后看看你正在传递的参数(无)…你能解释一下这些参数吗?我有点迷路了
    getLength(…)
    的目的是什么?它返回一个要求您输入的值。如果您对如何调用方法感到困惑,我强烈建议您阅读一本好书或教程。当你已经掌握了一门语言的基础知识时,堆栈溢出对于解决特定问题是很好的,但这不是开始学习一门语言的好方法。我尽可能地格式化了代码。似乎有一个错误,然后是一个单独的代码块,但我不确定拆分在哪里。