What's wrong with my code? Why aren't the scores changing from zero and how do I fix it?  package com.company;import java.util.ArrayList;import java.util.Collections;import java.util.Scanner;class Question {private String question;private String[] possibleAnswers;private int correctAnswer;// Constructorpublic Question(String question, String[] possibleAnswers, int correctAnswer) {this.question = question;this.possibleAnswers = possibleAnswers;this.correctAnswer = correctAnswer;}// Accessor methodspublic String getQuestion() {return question;}public String[] getPossibleAnswers() {return possibleAnswers;}public int getCorrectAnswer() {return correctAnswer;}}public class Main {public static void main(String[] args) {// Create an ArrayList to hold the trivia questionsArrayList<Question> triviaQuestions = new ArrayList<>();// Add 10 Question objects with trivia questionstriviaQuestions.add(new Question("What is the capital of France?",new String[]{"Berlin", "Madrid", "Paris", "Rome"}, 3));triviaQuestions.add(new Question("Which planet is known as the Red Planet?",new String[]{"Mars", "Jupiter", "Venus", "Saturn"}, 1));triviaQuestions.add(new Question("Who wrote 'Romeo and Juliet'?",new String[]{"William Shakespeare", "Jane Austen", "Charles Dickens", "Mark Twain"}, 1));triviaQuestions.add(new Question("What is the largest mammal in the world?",new String[]{"Elephant", "Blue Whale", "Giraffe", "Hippopotamus"}, 2));triviaQuestions.add(new Question("In which year did the Titanic sink?",new String[]{"1905", "1912", "1920", "1931"}, 2));triviaQuestions.add(new Question("What is the currency of Japan?",new String[]{"Yuan", "Dollar", "Yen", "Euro"}, 3));triviaQuestions.add(new Question("Who painted the Mona Lisa?",new String[]{"Vincent van Gogh", "Leonardo da Vinci", "Pablo Picasso", "Claude Monet"}, 2));triviaQuestions.add(new Question("What is the largest ocean on Earth?",new String[]{"Atlantic Ocean", "Indian Ocean", "Southern Ocean", "Pacific Ocean"}, 4));triviaQuestions.add(new Question("Which element has the chemical symbol 'O'?",new String[]{"Oxygen", "Gold", "Silver", "Iron"}, 1));triviaQuestions.add(new Question("What is the capital of Japan?",new String[]{"Beijing", "Tokyo", "Seoul", "Bangkok"}, 2));// Shuffle the questions to ensure each player gets different questionsCollections.shuffle(triviaQuestions);// Shuffle the questions to ensure each player gets different questionsCollections.shuffle(triviaQuestions);// Initialize player scores using Integer instead of intInteger player1Score = 0;Integer player2Score = 0;Scanner scanner = new Scanner(System.in);// Loop through each question for each playerfor (int i = 0; i < 5; i++) {Question questionForPlayer1 = triviaQuestions.get(i);Question questionForPlayer2 = triviaQuestions.get(i + 5);// Player 1's turnplayTurn("Player 1", questionForPlayer1, scanner, player1Score);// Player 2's turnplayTurn("Player 2", questionForPlayer2, scanner, player2Score);}scanner.close();// Display final scores and declare the winnerSystem.out.println("Player 1's score: " + player1Score);System.out.println("Player 2's score: " + player2Score);if (player1Score > player2Score) {System.out.println("Player 1 wins!");} else if (player1Score < player2Score) {System.out.println("Player 2 wins!");} else {System.out.println("It's a tie!");}}private static void playTurn(String playerName, Question question, Scanner scanner, Integer playerScore) {System.out.println("\n" + playerName + "'s turn - " + question.getQuestion());// Display possible answersfor (int i = 0; i < question.getPossibleAnswers().length; i++) {System.out.println((i + 1) + ". " + question.getPossibleAnswers()[i]);}// Get player's answerSystem.out.print(playerName + ", enter your answer (1-4): ");int playerAnswer = scanner.nextInt();// Check if the answer is correct and update scoreif (playerAnswer == question.getCorrectAnswer()) {System.out.println("Correct!");playerScore = playerScore + 1;} else {System.out.println("Incorrect. The correct answer is: " + question.getCorrectAnswer());}}}

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

What's wrong with my code? Why aren't the scores changing from zero and how do I fix it? 

package com.company;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class Question {
private String question;
private String[] possibleAnswers;
private int correctAnswer;

// Constructor
public Question(String question, String[] possibleAnswers, int correctAnswer) {
this.question = question;
this.possibleAnswers = possibleAnswers;
this.correctAnswer = correctAnswer;
}

// Accessor methods
public String getQuestion() {
return question;
}

public String[] getPossibleAnswers() {
return possibleAnswers;
}

public int getCorrectAnswer() {
return correctAnswer;
}
}

public class Main {
public static void main(String[] args) {
// Create an ArrayList to hold the trivia questions
ArrayList<Question> triviaQuestions = new ArrayList<>();

// Add 10 Question objects with trivia questions
triviaQuestions.add(new Question("What is the capital of France?",
new String[]{"Berlin", "Madrid", "Paris", "Rome"}, 3));

triviaQuestions.add(new Question("Which planet is known as the Red Planet?",
new String[]{"Mars", "Jupiter", "Venus", "Saturn"}, 1));

triviaQuestions.add(new Question("Who wrote 'Romeo and Juliet'?",
new String[]{"William Shakespeare", "Jane Austen", "Charles Dickens", "Mark Twain"}, 1));

triviaQuestions.add(new Question("What is the largest mammal in the world?",
new String[]{"Elephant", "Blue Whale", "Giraffe", "Hippopotamus"}, 2));

triviaQuestions.add(new Question("In which year did the Titanic sink?",
new String[]{"1905", "1912", "1920", "1931"}, 2));

triviaQuestions.add(new Question("What is the currency of Japan?",
new String[]{"Yuan", "Dollar", "Yen", "Euro"}, 3));

triviaQuestions.add(new Question("Who painted the Mona Lisa?",
new String[]{"Vincent van Gogh", "Leonardo da Vinci", "Pablo Picasso", "Claude Monet"}, 2));

triviaQuestions.add(new Question("What is the largest ocean on Earth?",
new String[]{"Atlantic Ocean", "Indian Ocean", "Southern Ocean", "Pacific Ocean"}, 4));

triviaQuestions.add(new Question("Which element has the chemical symbol 'O'?",
new String[]{"Oxygen", "Gold", "Silver", "Iron"}, 1));

triviaQuestions.add(new Question("What is the capital of Japan?",
new String[]{"Beijing", "Tokyo", "Seoul", "Bangkok"}, 2));

// Shuffle the questions to ensure each player gets different questions
Collections.shuffle(triviaQuestions);


// Shuffle the questions to ensure each player gets different questions
Collections.shuffle(triviaQuestions);

// Initialize player scores using Integer instead of int
Integer player1Score = 0;
Integer player2Score = 0;

Scanner scanner = new Scanner(System.in);

// Loop through each question for each player
for (int i = 0; i < 5; i++) {
Question questionForPlayer1 = triviaQuestions.get(i);
Question questionForPlayer2 = triviaQuestions.get(i + 5);

// Player 1's turn
playTurn("Player 1", questionForPlayer1, scanner, player1Score);

// Player 2's turn
playTurn("Player 2", questionForPlayer2, scanner, player2Score);
}

scanner.close();
// Display final scores and declare the winner
System.out.println("Player 1's score: " + player1Score);
System.out.println("Player 2's score: " + player2Score);

if (player1Score > player2Score) {
System.out.println("Player 1 wins!");
} else if (player1Score < player2Score) {
System.out.println("Player 2 wins!");
} else {
System.out.println("It's a tie!");
}
}

private static void playTurn(String playerName, Question question, Scanner scanner, Integer playerScore) {
System.out.println("\n" + playerName + "'s turn - " + question.getQuestion());

// Display possible answers
for (int i = 0; i < question.getPossibleAnswers().length; i++) {
System.out.println((i + 1) + ". " + question.getPossibleAnswers()[i]);
}

// Get player's answer
System.out.print(playerName + ", enter your answer (1-4): ");
int playerAnswer = scanner.nextInt();

// Check if the answer is correct and update score
if (playerAnswer == question.getCorrectAnswer()) {
System.out.println("Correct!");
playerScore = playerScore + 1;
} else {
System.out.println("Incorrect. The correct answer is: " + question.getCorrectAnswer());
}
}
}

AI-Generated Solution
AI-generated content may present inaccurate or offensive content that does not represent bartleby’s views.
steps

Unlock instant AI solutions

Tap the button
to generate a solution

Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education