投票类
package com.toupiao; //投票类 public class TouPiao { private String name; static boolean flag = true; static int voteNum; public void showVote() { voteNum++; if(voteNum >=100) { System.out.println("票数达到上限"); flag = false; }else { System.out.println("投票成功,当前票数为"+(voteNum+1)); } } public String getName() { return name; } public void setName(String name) { this.name = name; } }
测试类
package com.toupiao; //投票测试类 import java.util.Scanner; public class Main { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { TouPiao tp = new TouPiao(); // while(TouPiao.flag) { // System.out.print("请输入投票人的姓名:"); // String name = sc.next(); // tp.setName(name); // tp.showVote(); // //TouPiao.voteNum++; // } for(int i = 0;i<9999;i++) { String name="***"; tp.setName(name); tp.showVote(); if(TouPiao.flag == false) { break; } } } }