标签:nts 主程序 put sharp bsp private 生成 log stat
投票城市用到了:system.in, 正则pattern,matcher,排序接口comparable 复写compareTo排序方法
一个班级在选班长,按序号进行投票,并将票数最高的放在第一位显示
设计:
student学生类
投票菜单类
投票操作类
接受输入数据类
主程序调用
public class Student implements Comparable{
private int id;
private String name;
private int vote;
public Student(int id, String name, int vote) {
super();
this.id = id;
this.name = name;
this.vote = vote;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getVote() {
return vote;
}
public void setVote(int vote) {
this.vote = vote;
}
@Override
public String toString() {
return "姓名::" + name + ", 票数:" + vote + "]";
}
public int compareTo(Student stu) {
// TODO 自动生成的方法存根
if(this.vote > stu.vote)
{
return 1;
}else if(this.vote < stu.vote)
{
return -1;
}else{
return 0;
}
}
}
public class Menu {
private Student stu[] = {new Student(1,"张三",0), new Student(2,"李四",0), new Student(3,"王五",0), new Student(4,"田七",0)};
public Menu() throws IOException
{
Operater oper = new Operater(this.stu);
//输出全部需要投票的姓名
oper.list();
//投票
while(oper.vote())
{
;
}
//投票结束后,列出所有的飘数
oper.list();
}
}
ublic class Operater {
private Student stu[]=null;
private InputData inputData = null;
public Operater(Student stu[])
{
this.stu = stu;
this.inputData = new InputData();
}
public void list()
{
for(int i =0; i< this.stu.length; i++)
{
System.out.println(this.stu[i].getId()+"号,姓名:"+this.stu[i].getName()+":【"+this.stu[i].getVote()+"】票");
}
}
public boolean vote() throws IOException
{
boolean flag = true;
int stuno = this.inputData.getInt("请输入你要选择的班长候选人ID:", "抱歉您输入的数据有误");
switch(stuno)
{
case 1:{
this.stu[0].setVote( this.stu[0].getVote() + 1);
break;
}
case 2:{
this.stu[1].setVote( this.stu[1].getVote() + 1);
break;
}
case 3:{
this.stu[2].setVote( this.stu[2].getVote() + 1);
break;
}
case 4:{
this.stu[3].setVote( this.stu[3].getVote() + 1);
break;
}
case 0:
{
flag = false;
break;
}
default:
{
flag = false;
break;
}
}
return flag;
}
}
public class InputData {
/*public static void main(String[] args) throws IOException
{
InputStream ipt = System.in;
System.out.println("请输入:");
int temp=0;
StringBuffer buf = new StringBuffer();
while( (temp = ipt.read()) != -1 )
{
char c = (char)temp;
if(c == ‘\n‘)
{
break;
}
buf.append(c);
}
String IpuStr = new String(buf);
//IpuStr = IpuStr.matches("\\s*|\r|\n|\t");
System.out.println( replace(IpuStr) );
IpuStr = replace(IpuStr);
if(IpuStr.matches("^\\d$"))
{
System.out.println( new Integer(IpuStr) );
}else{
System.out.println( "0" );
}
}
public static String replace(String str)
{
if(str != null && !"".equals(str))
{
Pattern p = Pattern.compile("\\s*|\r|\n|\t");
Matcher m = p.matcher(str);
return m.replaceAll("");
}else{
return str;
}
}
*/
public String replace(String str)
{
if(str != null && !"".equals(str))
{
Pattern p = Pattern.compile("\\s*|\r|\n|\t");
Matcher m = p.matcher(str);
return m.replaceAll("");
}else{
return str;
}
}
public int getInt(String str, String str2) throws IOException
{
InputStream ipt = System.in;
System.out.println(str);
int temp=0;
StringBuffer buf = new StringBuffer();
while( (temp = ipt.read()) != -1 )
{
char c = (char)temp;
if(c == ‘\n‘)
{
break;
}
buf.append(c);
}
String IpuStr = new String(buf);
IpuStr = this.replace(IpuStr);
if(IpuStr.matches("\\d"))
{
return new Integer(IpuStr);
}else{
return 0;
}
}
}
public class Test {
public static void main(String[] args) throws IOException {
// TODO 自动生成的方法存根
new Menu();
}
}
标签:nts 主程序 put sharp bsp private 生成 log stat
原文地址:http://www.cnblogs.com/achengmu/p/7440357.html