代码无法编译,因为"实际和正式参数列表的长度不同



下面是我的代码:

public class RSubuntuPgm1 { 
public static void main (String arg[]){

System.out.printf("ngedit");
System.out.printf("nCtrl + S");
System.out.printf("nAlt + F4");
processUbuntu();
}
public void processUbuntu(String name, String date, double number){
name = "Sean";
number = 2021.99;
date = "Fall";
System.out.printf("n%s",name);
System.out.printf("n%.5f",number); // 10 spaces, 5 more spaces for decimals
System.out.printf("n%S",date); //capital S makes all capital
}
}

必须这样格式化,因为它是分级作业。这个错误是因为我试图调用void方法。

您正在调用processUbuntu();
,但方法签名是:processUbuntu(String name, String date, double number)
意味着它需要传递两个字符串和一个double。

Try:processUbuntu("Sean", "Fall", 2021.99);并去掉方法体中的赋值

问题中的实际错误是告诉您"processUbuntu需要3个参数,但您提供的是零"。以更一般的方式

请参阅David Lorenzo MARINO对下一个bug的回答。

你的代码中有一些错误。

  • 您需要将值传递给具有参数的函数(这是标题中提到的错误)
  • 如果不引用实例,就不能直接从静态方法调用实例方法

还有一些额外的提示:

  • 不要在processUbuntu函数中重新赋值

正确的代码应该是

public class RSubuntuPgm1 { 
public static void main(String arg[]) {
System.out.printf("ngedit");
System.out.printf("nCtrl + S");
System.out.printf("nAlt + F4");

// You need to pass the values for the parameters name, date, number
processUbuntu("Sean", "Fall", 2021.99);
}
// Added the static keyword
public static void processUbuntu(String name, String date, double number) {
// You don't need to reassign the values so the following lines are commented
// name = "Sean"; 
// number = 2021.99;
// date = "Fall";
System.out.printf("n%s",name);
System.out.printf("n%.5f",number); // 10 spaces, 5 more spaces for decimals
System.out.printf("n%S",date); //capital S makes all capital
}
}

最新更新