"实际的或正式的参数列表的不同之处长度QUOT; [英] "Actual or formal argument lists differs in length"

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

问题描述

当我尝试把一些在()的括号F =新朋友(friendsName,friendsAge)朋友;它与错误出现:朋友的构造上课的朋友不能适用于特定类型的需求:无参数实测值:字符串,整数原因:实际的或正式的参数列表的长度不同。但是,当我拿出论据我的好友列表只显示空0。都没有设置,即使我有字符串friendsName = input.next();值?

此外,当我尝试删除的朋友,它不会做任何事情。在源$ C ​​$ C它确实带来了一个警告,可疑呼叫util.java.Collection.remove:给定对象不能包含字符串(预期友)给出的实例。我在困惑是什么意思吗?

 进口的java.util.ArrayList;
进口java.util.Scanner中;公共类友
{
    公共静态无效的主要(字串[] args)
    {
        INT菜单;
        INT选择;
        选择= 0;        扫描仪输入=新的扫描仪(System.in);
        ArrayList的<朋友> friendsList =新的ArrayList< >();        的System.out.println(1.添加朋友);
        的System.out.println(2.删除好友);
        的System.out.println(3.显示所有好友);
        的System.out.println(4.退出);
        菜单= input.nextInt();        而(菜单!= 4)
        {            开关(菜单)
            {            情况1:                而(选择!= 2)
                {
                    的System.out.println(输入朋友的姓名:);
                    串friendsName = input.next();
                    的System.out.println(输入朋友的年龄:);
                    INT friendsAge = input.nextInt();
                    朋友F =新朋友(friendsName,friendsAge);
                    friendsList.add(F);
                    的System.out.println(输入另一个1:是,2:否);
                    选择= input.nextInt();
                }打破;            案例2:                的System.out.println(进入朋友的名字删除:);
                friendsList.remove(input.next());
                打破;            案例3:                的for(int i = 0; I< friendsList.size();我++)
                {
                    的System.out.println(friendsList.get(I)。名称++ friendsList.get(我)。年龄);
                }打破;
        }        的System.out.println(1.添加朋友);
        的System.out.println(2.删除好友);
        的System.out.println(3.显示所有好友);
        的System.out.println(4.退出);
        菜单= input.nextInt();    }    的System.out.println(谢谢你,再见!);}    公共字符串名称;
    公众诠释年龄;    公共无效setname可以(字符串friendsName)
    {
        名称= friendsName;
    }
    公共无效setAge(INT friendsAge)
    {
        年龄= friendsAge;
    }
    公共字符串的getName()
    {
        返回名称;
    }
    公众诠释getAge()
    {
        返回年龄;
    }
}


解决方案

您尝试实例化交友对象类是这样的:

 友F =新朋友(friendsName,friendsAge);

本类没有一个构造函数的参数。您应该添加构造函数,或者使用确实存在,然后使用设置方法构造函数创建对象。例如,代替上述

 友F =新朋友();
f.setName(friendsName);
f.setAge(friendsAge);

When I try to put something in the () brackets of "Friends f = new Friends(friendsName, friendsAge);" it comes up with the error: "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." 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, "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;
    }
}

解决方案

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

Friends f = new Friends(friendsName, friendsAge);

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

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

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