【问题标题】:Methods and return problems方法和退货问题
【发布时间】:2016-03-24 22:58:08
【问题描述】:

任何人都知道这个错误意味着什么:

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的参数...然后看看你传递的参数(无)...
  • 你能解释一下这些论点吗?我有点迷路了
  • getLength(...) 的目的是什么?它返回一个要求您输入的值。
  • 如果您对如何调用方法感到困惑,我强烈建议您阅读一本好书或教程。当您掌握一门语言的基础知识时,Stack Overflow 非常适合解决特定问题,但它不是开始学习一门语言的好方法。
  • 我尽可能地格式化了代码。好像有一个错误,然后有一个单独的代码块,但我不确定拆分在哪里。

标签: java methods area


【解决方案1】:

getLength 是一个 void 方法。如果要返回某些内容,则需要将 void 替换为数据类型 double 在您的情况下。您还需要确保传递正确的参数类型和正确数量的参数。

【讨论】:

    【解决方案2】:

    方法getLength()

    1. 它需要两个参数double aLengthScanner aKeyboard
    2. 您正在将此值分配给长度,即 double,但您没有从方法 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-20
      • 2014-06-10
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-31
      相关资源
      最近更新 更多