Greetings-I am having problems with the following code. Results are supposed to
ID: 3561993 • Letter: G
Question
Greetings-I am having problems with the following code. Results are supposed to be:
Enter your vote <Tichen:1, Orator:2, end:-1> 1
Enter your vote <Tichen:1, Orator:2, end:-1> 1
Enter your vote <Tichen:1, Orator:2, end:-1> 2
Enter your vote <Tichen:1, Orator:2, end:-1> 2
Enter your vote <Tichen:1, Orator:2, end:-1> 3
ERROR!! Please enter 1 or 2
Enter your vote <Tichen:1, Orator:2, end:-1> 1
Enter your vote <Tichen:1, Orator:2, end:-1> 2
Enter your vote <Tichen:1, Orator:2, end:-1> -1
Total Number of votes: 6
Tichen: 3 (50%)
Oraton: 3 (50%)
It is a Tie.
---------------------------------------------------------------
//below is my code
import java.utilScanner;
public class VoteTally4
{
public static void main (Strings [] args)
{
//declare variables
//number of votes for Tichen
int voteTichen;
//number of votes for Orator
int voteOrator;
//total votes for Tichen
int totalTichen;
//total votes for Orator
int totalOrator;
//winner
int winner;
//create scanner
scanner input = new Scanner(System.in);
//explain to voter how to vote
System.out.println("To cast your vote: Tichen = 1; Orator = 2, To Exit press '-1'");
//create switches
switch(vote)
{
case 1: vote tichen;
System.out.println("To cast your vote: Tichen press number 1");
System.out.println("Cast your vote, please");
voteTichen = scan.nextInt();
break;
case 2: vote orator;
system.out.println("To cast your vote for Orator press number 2");
System.out.println("Cast your vote, please");
voteOrator = scan.nextInt();
break;
}
{
else
System.out.println("To exist press number -1 as in negative one");
System.out.println("Press number -1, please");
}
//calculate results
if (totalTichen > totalOrator)
System.out.println("Tichen wins!");
else if(totalOrator > totalTichen)
System.out.println("Orator wins!");
}
else
{
System.out.println("Error, please enter correct number");
Scanner.out();
}
}
Explanation / Answer
***********************************USING SWITCH CASE*******************************************
import java.util.*;
public class VoteTally4 {
public VoteTally4() {
}
public static void main(String arg[])
{
//Enter your vote <Tichen:1, Orator:2, end:-1
float count_Tichen=0;
float count_Orator=0;
float total_vote=0;
int vote=-1;
Scanner Sc=new Scanner(System.in);
do
{
System.out.println("Enter your vote <Tichen:1, Orator:2, end:-1>");
vote=Sc.nextInt();
switch(vote)
{
case 1:
count_Tichen++;
total_vote++;
break;
case 2:
count_Orator++;
total_vote++;
break;
case -1:
break;
default:
System.out.println("ERROR!! Please enter 1 or 2");
break;
}
}while(vote!=-1);
System.out.println("Total Number of votes: ");
System.out.println("Tichen : "+count_Tichen+" ( "+(count_Tichen/total_vote)*100+" %)");
System.out.println("Orator : "+count_Orator+" ( "+(count_Orator/total_vote)*100+" %)");
if(count_Tichen>count_Orator)
System.out.println("Tichen is Winner");
if(count_Tichen<count_Orator)
System.out.println("Orator is Winner");
if(count_Orator==count_Tichen)
System.out.println("Its a Tie ");
}
}
**********************************************OUTPUT*****************************************************
--------------------Configuration: <Default>--------------------
Enter your vote <Tichen:1, Orator:2, end:-1>
1
Enter your vote <Tichen:1, Orator:2, end:-1>
1
Enter your vote <Tichen:1, Orator:2, end:-1>
2
Enter your vote <Tichen:1, Orator:2, end:-1>
2
Enter your vote <Tichen:1, Orator:2, end:-1>
3
ERROR!! Please enter 1 or 2
Enter your vote <Tichen:1, Orator:2, end:-1>
1
Enter your vote <Tichen:1, Orator:2, end:-1>
2
Enter your vote <Tichen:1, Orator:2, end:-1>
-1
Total Number of votes:
Tichen : 3.0 ( 50.0 %)
Orator : 3.0 ( 50.0 %)
Its a Tie
Process completed.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.