“实际或正式的参数列表在长度上有所不同”
当我尝试将某些内容放在()括号中时,会出现错误:Friends f = new Friends(friendsName, friendsAge);
构造函数 类中的好友 不能应用于给定类型。必需:无参数。已找到:字符串,整型。原因:实际或正式的参数列表在长度上有所不同。
但是当我拿出参数时,我的朋友列表只显示“null 0”。即使我有,这些值也没有设置吗?String friendsName = input.next();
此外,当我尝试删除朋友时,它不会做任何事情。在源代码中,它确实会显示一个警告,
对 util.java.Collection.remove 的可疑调用:给定对象不能包含 String(预期的好友)的给定实例。
我对这一切意味着什么感到困惑?
import java.util.ArrayList;
import java.util.Scanner;
public class Friends
{
public static void main( String[] args )
{
int menu;
int choice;
choice = 0;
Scanner input = new Scanner(System.in);
ArrayList< Friends > friendsList = new ArrayList< >();
System.out.println(" 1. Add a Friend ");
System.out.println(" 2. Remove a Friend ");
System.out.println(" 3. Display All Friends ");
System.out.println(" 4. Exit ");
menu = input.nextInt();
while(menu != 4)
{
switch(menu)
{
case 1:
while(choice != 2)
{
System.out.println("Enter Friend's Name: ");
String friendsName = input.next();
System.out.println("Enter Friend's Age: ");
int friendsAge = input.nextInt();
Friends f = new Friends(friendsName, friendsAge);
friendsList.add(f);
System.out.println("Enter another? 1: Yes, 2: No");
choice = input.nextInt();
} break;
case 2:
System.out.println("Enter Friend's Name to Remove: ");
friendsList.remove(input.next());
break;
case 3:
for(int i = 0; i < friendsList.size(); i++)
{
System.out.println(friendsList.get(i).name + " " + friendsList.get(i).age);
} break;
}
System.out.println(" 1. Add a Friend ");
System.out.println(" 2. Remove a Friend ");
System.out.println(" 3. Display All Friends ");
System.out.println(" 4. Exit ");
menu = input.nextInt();
}
System.out.println("Thank you and goodbye!");
}
public String name;
public int age;
public void setName( String friendsName )
{
name = friendsName;
}
public void setAge( int friendsAge )
{
age = friendsAge;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
}