我的项目有这个 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 找到:String,int,int原因: 在我的主类的第 7、8、9 行和第 15、16、17 行,
实际参数列表和形式参数列表的长度不同。voteDemo.java:47: 错误: 找不到符号选择 = kb.nextInt(); 在我的主要课程上