/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package studentvote;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*-2
* @author Saeed
*/
public class StudentVote {
static char casting;
static char x = 0;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
int total_vote = 0;
int a=0,b=0,c=0,w=0;
boolean valid_Number_vote=true;
System.out.print("Enter the total number of votes:");
total_vote=readNumber();
do{
if ((total_vote <1)|| (total_vote > 1000) ){
System.out.println("This is not a valid value.");
System.out.print("Please enter the total number of votes again:");
total_vote=readNumber();
}else{
valid_Number_vote=false;
}
}
while(valid_Number_vote);
//read votes
for(int i=1;i<=total_vote;i++){
System.out.print("Vote "+i+" is for candidate:");
readVote();
switch(x){
case 'A':
case 'a':
a++;
break;
case 'B':
case 'b':
b++;
break;
case 'C':
case 'c':
c++;
break;
case 'W':
case 'w':
w++;
break;
}//end of switch statement
} //end of vote counting
System.out.println("\nResults:");
System.out.println("Number of votes for candidate A: "+a);
System.out.println("Number of votes for candidate B: "+b);
System.out.println("Number of votes for candidate C: "+c);
System.out.println("Number of white votes: "+w);
}//end of class
/////////////////////////////////////////////////////////////////////////////////////////////////////
static int readNumber(){
int number=0;
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
number = Integer.parseInt(br.readLine());
} catch(Exception e){
System.out.println("Notice " +e.getMessage());
System.exit(0); }
return number;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
static void readVote(){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
x= br.readLine().toUpperCase().charAt(0);
} catch(Exception e){
System.out.println("Notice " +e.getMessage());
System.exit(0); }
//System.out.println(x);
if ((x=='A')||(x=='a')||(x=='B')||(x=='b')||(x=='C')||(x=='c')||(x=='W')||(x=='w')){
;
}
else{
System.out.println("There is no such candidate.");
System.out.print("Please enter one of characters ‘A’,‘B’,‘C’ or ‘W’: ");
readVote();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
}//end of class
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package studentvote;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*-2
* @author Saeed
*/
public class StudentVote {
static char casting;
static char x = 0;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
int total_vote = 0;
int a=0,b=0,c=0,w=0;
boolean valid_Number_vote=true;
System.out.print("Enter the total number of votes:");
total_vote=readNumber();
do{
if ((total_vote <1)|| (total_vote > 1000) ){
System.out.println("This is not a valid value.");
System.out.print("Please enter the total number of votes again:");
total_vote=readNumber();
}else{
valid_Number_vote=false;
}
}
while(valid_Number_vote);
//read votes
for(int i=1;i<=total_vote;i++){
System.out.print("Vote "+i+" is for candidate:");
readVote();
switch(x){
case 'A':
case 'a':
a++;
break;
case 'B':
case 'b':
b++;
break;
case 'C':
case 'c':
c++;
break;
case 'W':
case 'w':
w++;
break;
}//end of switch statement
} //end of vote counting
System.out.println("\nResults:");
System.out.println("Number of votes for candidate A: "+a);
System.out.println("Number of votes for candidate B: "+b);
System.out.println("Number of votes for candidate C: "+c);
System.out.println("Number of white votes: "+w);
}//end of class
/////////////////////////////////////////////////////////////////////////////////////////////////////
static int readNumber(){
int number=0;
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
number = Integer.parseInt(br.readLine());
} catch(Exception e){
System.out.println("Notice " +e.getMessage());
System.exit(0); }
return number;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
static void readVote(){
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
x= br.readLine().toUpperCase().charAt(0);
} catch(Exception e){
System.out.println("Notice " +e.getMessage());
System.exit(0); }
//System.out.println(x);
if ((x=='A')||(x=='a')||(x=='B')||(x=='b')||(x=='C')||(x=='c')||(x=='W')||(x=='w')){
;
}
else{
System.out.println("There is no such candidate.");
System.out.print("Please enter one of characters ‘A’,‘B’,‘C’ or ‘W’: ");
readVote();
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
}//end of class
No comments:
Post a Comment