2013-03-29 80 views
-2

我有這個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."); 

    } 
    } 

,我得到的錯誤:

required: String,int,int,double found: String,int,int
reason: actual and formal argument lists differ in length
on lines 7, 8, 9 and 15, 16, 17 on my main class.

voteDemo.java:47: error: cannot find symbol choice = kb.nextInt(); on my main class

+2

什麼問題?錯誤信息是什麼意思? –

+1

這是很多代碼。考慮[減少這個簡短的例子](http://meta.stackexchange.com/questions/22754/sscce-how-to-provide-examples-for-programming-questions),顯示相同的問題。 – Joe

回答

2

簡而言之,這意味着你有一個需要N個參數的方法調用,但是你給了它錯誤的參數個數;例如

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: error: cannot find symbol choice = kb.nextInt(); on my main class

您還沒有宣佈choice。你剛分配給你還沒有聲明的變量。在Java中不能這樣做。每個變量都必須顯式聲明。


雖然大家注意,它通常是一個壞主意把前導零的Java數值文字。爲什麼?因爲前導零告訴Java編譯器,您使用的是八進制而非十進制。所以011其實意味着九,而不是十一!除非你用表示以八進制表示你的數字,否則不要使用前導零。

+0

對不起,我可能會監督。但是,謝謝,我現在有較少的錯誤。 – Ella

相關問題