in Java how do you create a method that will prompt the user to type in 10 votes to determine a winner in a two candidate election?
Ex: Vote 10 times - Smith vs. Watson
User enters:
Watson
watson
wATson
Smith
SMITH
smiTh
watson
smith
watson
waTSoN
And then this should report that Watson wins 6 votes to 4 votes.
the names don't have to be Watson and Smith, any two names will do.
String inputVote;
int vote1 = 0;
int vote2 = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter your votes 10 times: ");
inputVote = keyboard.next();
if (inputVote.equalsIgnoreCase("Smith")){
vote1++;
}
if (inputVote.equalsIgnoreCase("Watson")){
vote2++;
}
inputVote = keyboard.next();
if (inputVote.equalsIgnoreCase("Smith")){
vote1++;
}
if (inputVote.equalsIgnoreCase("Watson")){
vote2++;
}
inputVote = keyboard.next();
if (inputVote.equalsIgnoreCase("Smith")){
vote1++;
}
if (inputVote.equalsIgnoreCase("Watson")){
vote2++;
}
inputVote = keyboard.next();
if (inputVote.equalsIgnoreCase("Smith")){
vote1++;
}
if (inputVote.equalsIgnoreCase("Watson")){
vote2++;
}
inputVote = keyboard.next();
if (inputVote.equalsIgnoreCase("Smith")){
vote1++;
}
if (inputVote.equalsIgnoreCase("Watson")){
vote2++;
}
inputVote = keyboard.next();
if (inputVote.equalsIgnoreCase("Smith")){
vote1++;
}
if (inputVote.equalsIgnoreCase("Watson")){
vote2++;
}
inputVote = keyboard.next();
if (inputVote.equalsIgnoreCase("Smith")){
vote1++;
}
if (inputVote.equalsIgnoreCase("Watson")){
vote2++;
}
inputVote = keyboard.next();
if (inputVote.equalsIgnoreCase("Smith")){
vote1++;
}
if (inputVote.equalsIgnoreCase("Watson")){
vote2++;
}
inputVote = keyboard.next();
if (inputVote.equalsIgnoreCase("Smith")){
vote1++;
}
if (inputVote.equalsIgnoreCase("Watson")){
vote2++;
}
inputVote = keyboard.next();
if (inputVote.equalsIgnoreCase("Smith")){
vote1++;
}
if (inputVote.equalsIgnoreCase("Watson")){
vote2++;
}
System.out.println("Smith votes: " + vote1);
System.out.println("Watson's votes: "+ vote2);
To create a method that prompts the user to type in 10 votes and determines a winner in a two-candidate election in Java, you can follow these steps:
1. Define a method with the desired functionality. For example, you can create a method called `determineWinner()`.
2. Inside the `determineWinner()` method, start by declaring two integer variables to keep track of the number of votes for each candidate. For instance, you can have variables `votesForWatson` and `votesForSmith`, both initialized to 0.
3. Use a loop that iterates 10 times to prompt the user for each vote. You can use a `for` loop with a loop counter `i` ranging from 1 to 10.
4. Inside the loop, use `System.out.println()` to display a prompt asking the user to enter a vote. For example, `Enter vote #i:`.
5. Use the `Scanner` class to read the voter's input. Create a `Scanner` object to read from the standard input stream (`System.in`).
6. Compare the vote received from the user with the names of the candidates to increment the respective vote counters. Use an `if` statement to check whether the vote matches either candidate's name.
7. Convert the vote to lowercase (if needed) to make the comparison case-insensitive. You can use the `toLowerCase()` method of the `String` class.
8. After the loop completes, check which candidate received more votes using an `if` statement. Print a message displaying the number of votes each candidate received and declare the winner.
Here's an example implementation:
```java
import java.util.Scanner;
public class Election {
public static void main(String[] args) {
determineWinner();
}
public static void determineWinner() {
int votesForWatson = 0;
int votesForSmith = 0;
Scanner scanner = new Scanner(System.in);
for (int i = 1; i <= 10; i++) {
System.out.print("Enter vote #" + i + ": ");
String vote = scanner.nextLine();
vote = vote.toLowerCase(); // Convert vote to lowercase for case-insensitive comparison
if (vote.equals("watson")) {
votesForWatson++;
} else if (vote.equals("smith")) {
votesForSmith++;
}
}
System.out.println("Watson: " + votesForWatson + " votes");
System.out.println("Smith: " + votesForSmith + " votes");
if (votesForWatson > votesForSmith) {
System.out.println("Watson wins!");
} else if (votesForWatson < votesForSmith) {
System.out.println("Smith wins!");
} else {
System.out.println("It's a tie!");
}
}
}
```