我有這個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
什麼問題?錯誤信息是什麼意思? –
這是很多代碼。考慮[減少這個簡短的例子](http://meta.stackexchange.com/questions/22754/sscce-how-to-provide-examples-for-programming-questions),顯示相同的問題。 – Joe