学习班级班干部投票专用代码

package Candidate;
学习班级班干部投票专用代码
  1. import java.util.Scanner;

  2.  
  3. public class OneTest {

  4.  
  5. @SuppressWarnings({ "resource",})

  6. public static void main(String[] args) {

  7. Candidate[] candidates={new Candidate("张三",1,0),new Candidate("李四",2,0),new Candidate("王五",3,0),new Candidate("赵六",4,0)};

  8. System.out.println("1:张三【0票】");

  9. System.out.println("2:李四【0票】");

  10. System.out.println("3:王五【0票】");

  11. System.out.println("4:赵六【0票】");

  12. Scanner intScanner=new Scanner(System.in);

  13. System.out.print("请输入班长候选人代号(数字0结束):");

  14.  
  15. String numStr=intScanner.nextLine();

  16. boolean isNum=false;

  17. do{

  18. //System.out.println("输入的内容是:"+intScanner.next());

  19. try{

  20. int num=Integer.valueOf(numStr);

  21. isNum=true;

  22. if(num==0){

  23. break;

  24. }else if(num==1){

  25. candidates[0].setBallot(candidates[0].getBallot()+1);

  26. }else if(num==2){

  27. candidates[1].setBallot(candidates[1].getBallot()+1);

  28. }else if(num==3){

  29. candidates[2].setBallot(candidates[2].getBallot()+1);

  30. }else if(num==4){

  31. candidates[3].setBallot(candidates[3].getBallot()+1);

  32. }else{

  33. System.out.println("此选票无效,请输入正确的候选人代号!");

  34. }

  35. System.out.print("请输入班长候选人代号(数字0结束):");

  36. numStr=intScanner.nextLine();

  37. } catch (Exception e) {

  38. System.out.println("此选票无效http://www.aivote.com请输入正确的候选人代号!");

  39. System.out.print("请输入班长候选人代号(数字0结束):");

  40. numStr=intScanner.nextLine();

  41. main(args);

  42. }

  43. }while (isNum);

  44.  
  45.  
  46. System.out.println("张三:"+candidates[0].getBallot()+"票");

  47. System.out.println("李四:"+candidates[1].getBallot()+"票");

  48. System.out.println("王五:"+candidates[2].getBallot()+"票");

  49. System.out.println("赵六:"+candidates[3].getBallot()+"票");

  50.  
  51. Candidate tempcaCandidate=candidates[0];

  52. for(int i=0;i<candidates.length-1;i++){

  53. if(candidates[i].getBallot()<candidates[i+1].getBallot()){

  54. tempcaCandidate=candidates[i+1];

  55. }

  56.  
  57. }

  58. System.out.print(tempcaCandidate.getSymbol());

  59. System.out.print(":"+tempcaCandidate.getName());

  60. System.out.print(":"+tempcaCandidate.getBallot()+"票");

  61.  
  62. }

  63. }

Candidate候选人对象

 
 
  1. package Candidate;

  2. public class Candidate {

  3. private String name;

  4. private int symbol;

  5. private int ballot;

  6. public String getName() {

  7.  
  8. return name;

  9. }

  10.  
  11. public void setName(String name) {

  12.  
  13. this.name = name;

  14. }

  15. public int getSymbol() {

  16.  
  17. return symbol;

  18. }

  19. public void setSymbol(int symbol) {

  20. this.symbol = symbol;

  21. }

  22. public int getBallot() {

  23. return ballot;

  24. }

  25. public void setBallot(int ballot) {

  26. this.ballot = ballot;

  27. }

  28. public Candidate(){}

  29. public Candidate(String name, int symbol, int ballot) {

  30. super();

  31. this.name = name;

  32. this.symbol = symbol;

  33. this.ballot = ballot;

  34. }

  35. }

猜你喜欢

转载自blog.csdn.net/qq_42595119/article/details/82561323