public class ExecDemo{
public static void main(String args[]){
new Operate() ;
}
};
import java.io.BufferedReader ;
import java.io.InputStreamReader ;
import java.io.IOException ;
public class InputData{
private BufferedReader buf = null ; // 接收数据
public InputData(){
this.buf = new BufferedReader(new InputStreamReader(System.in)) ;
}
public String getString(String info){ // 得到字符串
String temp = null ; // 接收输入内容
System.out.print(info) ;
try{
temp = this.buf.readLine() ; // 接收数据
}catch(IOException e){
e.printStackTrace() ;
}
return temp ;
}
public int getInt(String info,String err){ // 得到整型数据
int temp = 0 ;
String str = null ;
boolean flag = true ; // 定义一个循环标记
while(flag){
str = this.getString(info) ;
if(str.matches("\\d+")){
temp = Integer.parseInt(str) ;
flag = false ; // 更改标志位,将退出循环
}else{
System.out.println(err) ;
}
}
return temp ;
}
};
public class Operate{
private Student stu[] = {new Student(1,"张三",0),new Student(2,"李四",0),
new Student(3,"王五",0),new Student(4,"赵六",0)} ;// 侯选人信息
private boolean flag = true ;
public Operate(){
this.printInfo() ; // 先输出候选人信息
while(flag){
this.vote() ; // 循环调用投票
}
this.printInfo() ; // 输出投票之后的侯选人信息
this.getResult() ; // 得到结果
}
private void getResult(){ // 得到最终的投票结果
java.util.Arrays.sort(this.stu) ; // 排序
System.out.println("投票最终结果:" + this.stu[0].getName()+"同学,最后以"+this.stu[0].getVote()+"票当选班长!") ;
}
public void vote(){ // 此方法完成投票功能
InputData input = new InputData() ; // 输入数据
int num = input.getInt("请输入班长侯选人代号(数字0结束):","此选票无效,请输入正确的侯选人代号!") ;
switch(num){
case 0:{
this.flag = false ; // 中断循环
break ;
}
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 ;
}
default:{
System.out.println("此选票无效,请输入正确的候选人代号!") ;
}
}
}
public void printInfo(){
for(int i=0;i<stu.length;i++){
System.out.println(this.stu[i].getStuNo() + ":"
+ this.stu[i].getName() + "【"+this.stu[i].getVote()+"】") ;
}
}
};
 public class Student implements Comparable<Student>{
private int stuNo ; // 学生编号
private String name ; // 学生姓名
private int vote ; // 学生票数
public Student(int stuNo,String name,int vote){
this.setStuNo(stuNo) ;
this.setName(name) ;
this.setVote(vote) ;
}
public int compareTo(Student o){
if(this.vote<o.vote){
return 1 ;
}else if(this.vote>o.vote){
return -1 ;
}else{
return 0 ;
}
}
public void setStuNo(int stuNo){
this.stuNo = stuNo ;
}
public void setName(String name){
this.name = name ;
}
public void setVote(int vote){
this.vote = vote ;
}
public int getStuNo(){
return this.stuNo ;
}
public String getName(){
return this.name ;
}
public int getVote(){
return this.vote ;
}
};

吴裕雄--天生自然JAVAIO操作学习笔记:投票程序的更多相关文章

  1. 吴裕雄--天生自然JAVAIO操作学习笔记:单人信息管理程序

    import java.io.* ; public class ExecDemo03{ public static void main(String args[]) throws Exception{ ...

  2. 吴裕雄--天生自然JAVAIO操作学习笔记:字符编码与对象序列化

    public class CharSetDemo01{ public static void main(String args[]){ System.out.println("系统默认编码: ...

  3. 吴裕雄--天生自然JAVAIO操作学习笔记:压缩流与回退流

    import java.io.File ; import java.io.FileInputStream ; import java.io.InputStream ; import java.util ...

  4. 吴裕雄--天生自然JAVAIO操作学习笔记:IO操作实例、Scanner、数据操作流与合并流

    import java.io.* ; public class ExecDemo01{ public static void main(String args[]) throws Exception{ ...

  5. 吴裕雄--天生自然JAVAIO操作学习笔记:字节流与字符流操作

    import java.io.* ; public class Copy{ public static void main(String args[]){ if(args.length!=2){ // ...

  6. 吴裕雄--天生自然JAVAIO操作学习笔记:RandomAccessFile

    import java.io.File ; import java.io.RandomAccessFile ; public class RandomAccessFileDemo01{ // 所有的异 ...

  7. 吴裕雄--天生自然JAVAIO操作学习笔记:File类

    import java.io.File ; import java.io.IOException ; public class FileDemo01{ public static void main( ...

  8. 吴裕雄--天生自然JAVAIO操作学习笔记:System类对IO的支持和BuffereRead

    import java.io.OutputStream ; import java.io.IOException ; public class SystemDemo01{ public static ...

  9. 吴裕雄--天生自然JAVAIO操作学习笔记:内存操作流、管道流与打印流操作

    import java.io.* ; class Send implements Runnable{ // 线程类 private PipedOutputStream pos = null ; // ...

随机推荐

  1. C语言:将字符串中的字符逆序输出,但不改变字符串中的内容。-在main函数中将多次调用fun函数,每调用一次,输出链表尾部结点中的数据,并释放该结点,使链表缩短。

    //将字符串中的字符逆序输出,但不改变字符串中的内容. #include <stdio.h> /************found************/ void fun (char ...

  2. 解决Zookeeper无法启动的问题

    在启动zookeeper集群的单个zookeeper节点时总是报如下错误 [root@zookeeper1 zookeeper-3.4.5]# bin/zkServer.sh status JMX e ...

  3. XMPP详解

    https://www.jianshu.com/p/84d15683b61e https://www.cnblogs.com/lurenq/p/7026983.html 1. xmpp简介 XMPP ...

  4. 【代码总结】PHP面向对象之类与对象

    一.类和对象的关系 类的实体化结果是对象,而对象的抽象就是类.在开发过程中,我们通常都是先抽象(幻想)出一个类,再用该类去创建对象(实现幻想的内容).在程序中,直接使用的是我们(实现幻想)的对象,而不 ...

  5. Codeforces Round #619 (Div. 2) Ayoub's function

    Ayoub thinks that he is a very smart person, so he created a function f(s)f(s) , where ss is a binar ...

  6. java中使用静态字段和构造函数跟踪某个类所创建对象的个数

    对于这个问题,我们都知道java中使用类时会自动调用构造函数.按照这个思路我们可以定义一个static int 形的常量count 然后将count++放入这个类的构造函数中,这样只要输出count的 ...

  7. 牛客大数加法-A+B

    题目描述实现一个加法器,使其能够输出a+b的值.输入描述:输入包括两个数a和b,其中a和b的位数不超过1000位.输出描述:可能有多组测试数据,对于每组数据,输出a+b的值.示例1输入2 610000 ...

  8. redis在Windows上启动报错

    The Windows version of Redis allocates a memory mapped heap for sharing with the forked process used ...

  9. mybatis升级案例之CRUD操作

    mybatis升级案例之CRUD操作 一.准备工作 1.新建maven工程,和入门案例一样 主要步骤如下,可参考mybatis入门实例 a.配置pom.xml文件 b.新建实例类User.DAO接口类 ...

  10. 二叉树(3)AVL 树

    封装基于 BinaryTreeOperations 的 AVL 树(一种自平衡的二叉查找树). 除了提供 BinaryTreeOperations 中的部分基础接口外,增加按键的插入 和 按键或节点指 ...