“实际或形式参数列表的长度不同" [英] "Actual or formal argument lists differs in length"

查看:43
本文介绍了“实际或形式参数列表的长度不同"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试在 Friends f = new Friends(friendsName,friendsAge); 的 () 括号中放一些东西时,它出现了错误:

When I try to put something in the () brackets of Friends f = new Friends(friendsName, friendsAge); it comes up with the error:

类 Friends 中的构造函数 Friends 不能应用于给定类型.必需:没有参数.找到:字符串,整数.原因:实际或正式参数列表的长度不同.

Constructor Friends in class Friends cannot by applied to given types. Required: no arguments. Found: String, int. Reason: actual or formal argument lists differ in length.

但是当我取出参数时,我的朋友列表只显示null 0".即使我有 StringfriendsName = input.next(); 也没有设置值吗?

But when I take out the arguments my friends list only displays "null 0". Are the values not set even though I have String friendsName = input.next();?

此外,当我尝试删除朋友时,它没有任何作用.在源代码中它确实提出了一个警告,

Also, when I try to remove a friend, it doesn't do anything. In the source code it does bring up a warning,

对 util.java.Collection.remove 的可疑调用:给定的对象不能包含给定的字符串实例(预期的朋友).

Suspicious call to util.java.Collection.remove: Given object cannot contain given instances of String (expected Friends).

我对这一切意味着什么感到困惑?

I'm confused on what that all means?

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

推荐答案

您尝试像这样实例化 Friends 类的对象:

You try to instantiate an object of the Friends class like this:

Friends f = new Friends(friendsName, friendsAge);

该类没有带参数的构造函数.您应该添加构造函数,或者使用确实存在的构造函数创建对象,然后使用 set-methods.例如,代替上面的:

The class does not have a constructor that takes parameters. You should either add the constructor, or create the object using the constructor that does exist and then use the set-methods. For example, instead of the above:

Friends f = new Friends();
f.setName(friendsName);
f.setAge(friendsAge);

这篇关于“实际或形式参数列表的长度不同"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆