package Candidate;
import java.util.Scanner;
public class OneTest {
@SuppressWarnings({ "resource",})
public static void main(String[] args) {
Candidate[] candidates={new Candidate("张三",1,0),new Candidate("李四",2,0),new Candidate("王五",3,0),new Candidate("赵六",4,0)};
System.out.println("1:张三【0票】");
System.out.println("2:李四【0票】");
System.out.println("3:王五【0票】");
System.out.println("4:赵六【0票】");
Scanner intScanner=new Scanner(System.in);
System.out.print("请输入班长候选人代号(数字0结束):");
String numStr=intScanner.nextLine();
boolean isNum=false;
do{
//System.out.println("输入的内容是:"+intScanner.next());
try{
int num=Integer.valueOf(numStr);
isNum=true;
if(num==0){
break;
}else if(num==1){
candidates[0].setBallot(candidates[0].getBallot()+1);
}else if(num==2){
candidates[1].setBallot(candidates[1].getBallot()+1);
}else if(num==3){
candidates[2].setBallot(candidates[2].getBallot()+1);
}else if(num==4){
candidates[3].setBallot(candidates[3].getBallot()+1);
}else{
System.out.println("此选票无效,请输入正确的候选人代号!");
}
System.out.print("请输入班长候选人代号(数字0结束):");
numStr=intScanner.nextLine();
} catch (Exception e) {
System.out.println("此选票无效,请输入正确的候选人代号!");
System.out.print("请输入班长候选人代号(数字0结束):");
numStr=intScanner.nextLine();
main(args);
}
}while (isNum);
System.out.println("张三:"+candidates[0].getBallot()+"票");
System.out.println("李四:"+candidates[1].getBallot()+"票");
System.out.println("王五:"+candidates[2].getBallot()+"票");
System.out.println("赵六:"+candidates[3].getBallot()+"票");
Candidate tempcaCandidate=candidates[0];
for(int i=0;i<candidates.length-1;i++){
if(candidates[i].getBallot()<candidates[i+1].getBallot()){
tempcaCandidate=candidates[i+1];
}
}
System.out.print(tempcaCandidate.getSymbol());
System.out.print(":"+tempcaCandidate.getName());
System.out.print(":"+tempcaCandidate.getBallot()+"票");
}
}
Candidate候选人对象
package Candidate;
public class Candidate {
private String name;
private int symbol;
private int ballot;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSymbol() {
return symbol;
}
public void setSymbol(int symbol) {
this.symbol = symbol;
}
public int getBallot() {
return ballot;
}
public void setBallot(int ballot) {
this.ballot = ballot;
}
public Candidate(){}
public Candidate(String name, int symbol, int ballot) {
super();
this.name = name;
this.symbol = symbol;
this.ballot = ballot;
}
}