实际和参数列表的长度不同

时间:2013-03-29 05:11:57

标签: java oop

我的项目有OOP程序,我无法理解为什么会出错。

这个程序就像一个投票系统,但只是简单地使用键盘输入。但在此之前,我想分别使用Voter vote = new Voter("String here");Candidates cand = new Candidates("String");来展示以前的选民,然后是候选人及其排名。

无论我如何再次查看我的代码,我仍然会遇到同样的错误。我是Java的新手,如果有人可以同时解释和回答我会有所帮助。如果有人看到我说的错误,它会很棒。谢谢!

我的代码,

候选班级:

    public class Candidates
{
    public String candName;
    private int position;
    private int totalVotes;

    public void Candidate (String candName, int position, int totalVotes)
    {
        this.candName = candName;
        this.position = position;
        this.totalVotes = totalVotes;
    }

    public void setDetails (String candName, int position, int totalVotes)
    {
        this.candName = candName;
        this.position = position;
        this.totalVotes = totalVotes;
    }

    public String getCandName()
    {
        return candName;
    }

    public int getPosition()
    {
        return position;
    }

    public int getTotalVotes()
    {
        return totalVotes;
    }
}

选民班:

    public class Voter
{
    private String name;
    private int votNum;
    private int precint;

    public Voter(String name, int votNum, int precint, double bDay)
    {
        this.name = name;
        this.votNum = votNum;
        this.precint = precint;
    }

    public void setDetails(String name, int votNum, int precint)
    {
        this.name = name;
        this.votNum = votNum;
        this.precint = precint;
    }

    public String getName()
    {
        return name;
    }

    public int getVotNum()
    {
        return votNum;
    }

    public int getPrecint()
    {
        return precint;
    }


    public Voter toString()
    {
        StringBuilder sb = new StringBuilder();
        sb.append(name).append(" ");
        sb.append(votNum).append(" ");
        sb.append(precint).append(" ");
         sb.append("Voter's Name: ").append(" ");
         sb.append("Voter's ID number: ").append(" ");
         sb.append("Precint: ").append(" ");
         return sb.toString();
    }
}

主要班级:

    import java.util.Scanner;

public class voteDemo
{
    public static void main(String[] args)
    {
        System.out.println("Previous voter's info: ");
        Voter vot1 = new Voter("Name1", 131, 01);
        Voter vot2= new Voter("Name2", 265, 02);
        Voter vot3= new Voter("Name3", 343, 01);
    System.out.println(vot1);
    System.out.println(vot2);
    System.out.println(vot3);

    System.out.println("The Candidates: ");
        Candidates cand1  = new Candidates("Candidate1", 1, 19000);
        Candidates cand2 = new Candidates("Candidate2" , 2, 17000);
        Candidates cand3 = new Candidates("Candidate3", 3, 12000);
    System.out.println(cand1);
    System.out.println(cand2);
    System.out.println(cand3);

    Scanner kb = new Scanner(System.in);

        System.out.println("Enter Voter's Name: ");
        String name = kb.nextLine();
        System.out.println("Enter Voter's ID: ");
        int votNum = kb.nextInt();
        System.out.println("Enter Precint: ");
        int precint = kb.nextInt();

        do
        {
            System.out.println("\n\nSelect Candidate for Senator:");
            System.out.println("1 - Choice1");
            System.out.println("2 - Choice2");
            System.out.println("3 - Choice3");
            System.out.println("4 - Choice4");
        System.out.println("5 - Choice5");
        System.out.print("\nEnter choice: ");

            choice = kb.nextInt();

           switch(choice)
            {
                case 1:
                        System.out.println("Name: " + name);
                        System.out.println("Voter ID: " + votNum);
                        System.out.println("Precint No.: " + precint);
                        System.out.println("Senator of choice: Choice1");
                        break;
                case 2:
                        System.out.println("Name: " + name);
                        System.out.println("Voter ID: " + votNum);
                        System.out.println("Precint No.: " + precint);
                        System.out.println("Senator of choice: Choice2");
                        break;
                case 3:
                        System.out.println("Name: " + name);
                        System.out.println("Voter ID: " + votNum);
                        System.out.println("Precint No.: " + precint);
                        System.out.println("Senator of choice: Choice3");
                        break;
                case 4:
                        System.out.println("Name: " + name);
                        System.out.println("Voter ID: " + votNum);
                        System.out.println("Precint No.: " + precint);
                        System.out.println("Senator of choice: Choice4");
                        break;
                case 5:
                        System.out.println("Name: " + name);
                        System.out.println("Voter ID: " + votNum);
                        System.out.println("Precint No.: " + precint);
                        System.out.println("Senator of choice: Choice5");
                        break;

                default:
                        System.out.println("Error. Review your entries.");
                        break;
            }
        } while (choice != 5 );

    System.out.println("Press Enter to confirm.");

    }
    }

我得到的错误:

  

必需:String,int,int,double found:String,int,int
   原因:实际和正式的参数列表长度不同
   在我的主要班级的第7,8,9和15,16,17行。

     

voteDemo.java:47:错误:找不到符号                         choice = kb.nextInt();    在我的主要班级

1 个答案:

答案 0 :(得分:2)

简单来说,这意味着你有一个需要N个参数的方法调用,但你给它的参数数量是错误的; e.g。

    public void setFoo(int arg) { ... }

    // using it correctly
    setFoo(24);

    // using it incorrectly
    setFoo();                     // compilation error - wrong number of args
    setFoo(42, 43);               // compilation error - wrong number of args

您可以使用构造函数和new获得相同的功能......正如您所做的那样。

public Voter(String name, int votNum, int precint, double bDay)

Voter vot1 = new Voter("Name1", 131, 01);

请参阅?

您使用4个参数声明它,并尝试将其与3一起使用。


这一个......

  

voteDemo.java:47:错误:找不到符号选择= kb.nextInt();在我的主要班级

您尚未声明choice。您刚刚分配了一个尚未声明的变量。不能用Java做到这一点。必须显式声明每个变量。


虽然我引起了你的注意,但在Java数字文字中加上前导零通常一个坏主意。为什么?因为前导零告诉Java编译器您使用的是八进制而不是十进制。所以011 实际上意味着九个,而不是十一个!除非表示以八进制编写数字,否则不要使用前导零。