Java IO _ instance operation: voting program notes

Source: Internet
Author: User

Java IO _ instance operation: VotingProgramNotes

 
Public class student implements comparable <student> {private int stuno; // student ID private string name; // Student name private int vote; // number of student votes 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 ;}};
Import Java. io. bufferedreader; import Java. io. inputstreamreader; import Java. io. ioexception; public class inputdata {private bufferedreader Buf = NULL; // receives public inputdata () {This. buf = new bufferedreader (New inputstreamreader (system. in);} Public String getstring (string info) {// obtain the string temp = NULL; // receives the input content system. out. print (Info); try {temp = This. buf. readline (); // receives data} catch (ioexception e) {e. printstacktrace ();} return temp;} public int getint (string info, string ERR) {// get int temp = 0; string STR = NULL; Boolean flag = true; // define a loop mark while (FLAG) {STR = This. getstring (Info); If (Str. matches ("\ D +") {temp = integer. parseint (STR); flag = false; // change the flag to exit the loop} else {system. out. println (ERR) ;}} return temp ;}};

Public class operate {private student STU [] = {New Student (1, "Zhang San", 0), new student (2, "Li Si", 0), new student (3, "Wang Wu", 0), new student (4, "Zhao Liu", 0)}; // The candidate information private Boolean flag = true; Public operate () {This. printinfo (); // first output the candidate information while (FLAG) {This. vote (); // round robin call voting} This. printinfo (); // output the candidate information after voting this. getresult (); // get result} private void getresult () {// get the final voting result Java. util. arrays. sort (this. stu ); // Sort system. out. println ("Final Result:" + this. STU [0]. getname () + "students, and finally" + this. STU [0]. getvote () + "Vote elected as Shift Leader! ");} Public void vote () {// This method completes the voting function inputdata input = new inputdata (); // the input data int num = input. getint ("Enter the candidate code of the shift leader (number 0):", "this vote is invalid. Enter the correct candidate code! "); Switch (Num) {Case 0: {This. flag = false; // interrupt cycle 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 ("this vote is invalid. Enter the correct candidate code! ") ;}} 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 execdemo {public static void main (string ARGs []) {New operate ();}};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.