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