```package com.amao.zuoye1107; import java.util.Scanner; public class PersonTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num1 = 0;//用于记录每位候选人的票数 int num2 = 0; int num3 = 0; Person p1 = new Person("张三",1,num1); Person p2 = new Person("李四",2,num2); Person p3 = new Person("王五",3,num3); Person person[] = new Person[]{p1,p2,p3}; System.out.println("姓名t序号t票数"); for(int i = 0; i< person.length; i++){ System.out.println(person[i]); } boolean isFlage = true;//设置标志位 int count= 0;//记录投票次数 while(isFlage) { count++; System.out.print("请投票:"); int num = sc.nextInt(); switch (num) { case 1: p1.setPiao(++num1); break; case 2: p2.setPiao(++num2); break; case 3: p3.setPiao(++num3); break; default: try { throw new RuntimeException("你输入的数据有误!"); }catch(Exception e){ System.out.println(e.getMessage()); }finally{ count--; } } if(count == 10){ isFlage = false;//投票次数够10次,修改标志位,退出循环 } } //显示被投票人信息 for(int i = 0; i < person.length; i++){ System.out.println(person[i]); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)