lundi 7 septembre 2020

Exiting Loop after first iteration; Reading from text file

I'm creating a trivia game for my friends to play to practice coding with Java. I have a text file formatted like (through 10):

    1
    What are the cheapest seats at a bull fight?
    A. At the top of the ring. 
    B. Closest to the ring.
    C. In the sun.
    D. In the Shade.
    C
    2
    What countries are located on the Iberian Peninsula?
    A. Spain and Portugal
    B. Ireland and Northern Ireland
    C. Bosnia and Herzegovinia
    D. Norway, Finland and Sweden
     A
    3
    What was the name of the person Mario saves in his video game debut "Donkey Kong"?
    A. Daisy
    B. Peach
    C. Pauline
    D. Caroline
    C

When I run my program, only the first person gets to answer and if they are right, they win. If they are incorrect, the other person wins. I want to alternate questions between the players and determine who wins at the end. I tried changing the numbering in the text file, but if I change the first "1" to "10" for instance, it only shows the 10th question, 10 times. I really appreciate the assistance. I'd like to have this sorted by next weekend for our weekly trivia games. Also, for some reason, in my questions class, Eclipse made me change my initialization of my variables to static My code is as follows:

From the Questions class:

    //initializing strings for the question
static String question;
//Creating Answers
static String answera;
static String answerb;
static String answerc;
static String answerd;
//creating correct answer
static String correctAnswer;

From my main program:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Trivia_Mull {

public static void main(String[] args) throws FileNotFoundException {

    ArrayList<Questions_Mull> triviaQuestions = new ArrayList<>();


    //Scanner to read file 
    File questionsFile = new File("triviaQuestions.txt");
    Scanner sc = new Scanner(questionsFile);
    
    //Scanner for answer input
    Scanner answer = new Scanner(System.in);
    
    
    int questionCount = Integer.parseInt(sc.nextLine());
    for(int j = 0; j < questionCount; j++) {
       triviaQuestions.add(new Questions_Mull(sc.nextLine(), sc.nextLine(),
               sc.nextLine(), sc.nextLine(), sc.nextLine(),
               sc.nextLine())); }
    
    String playerOneName, playerTwoName = null;
    int playerOneScore = 0;
    int playerTwoScore = 0;
 
       
    System.out.println("Welcome to Trivia!");
    System.out.println("Player One, Please Enter your name: ");
    playerOneName = answer.nextLine();
    System.out.println("Thank you, " +playerOneName +".");
    System.out.println("Player Two, Please Enter your name: ");
    playerTwoName = answer.nextLine();
    System.out.println("Thank you, "+playerTwoName +".");
    System.out.println();
    System.out.println("Ready? Let's Begin!");
    System.out.println();
    
    
    for(int i = 0; i < triviaQuestions.size(); i++) {
        if(i % 2 == 0) {
            System.out.println(playerOneName + ", please answer the following: ");
            printQuestion(triviaQuestions.get(i));
            String response = answer.next();
            if(response.equalsIgnoreCase(triviaQuestions.get(i).correctAnswer)) {
                playerOneScore++;
                System.out.println("Sweet! You have "
                        + playerOneScore + " points.\n");
            } else {
                System.out.println("Oh no! No points there! You currently have "
                        + playerOneScore + " points.\n");
                }
            }   else{ //Give alternation questions to Player Two
                System.out.println(playerTwoName +", please answer the following"
                        + " question:\n");
                printQuestion(triviaQuestions.get(i));
                String response = answer.next();
                
                if(response.equalsIgnoreCase(triviaQuestions.get(i).correctAnswer)) {
                    playerTwoScore++;
                    System.out.println("Rad! You have "
                            + playerTwoScore + " points.\n");
                } else {
                    System.out.println("Rats! That wasn't it, but you currently have "
                            + playerTwoScore + " points.\n");
                }
            }
        }
    
    //Drum roll, please! Determine who won
    if(playerOneScore > playerTwoScore) {
        System.out.print(playerOneName+" wins!");
    } else {
        System.out.print(playerTwoName+" wins!");
    }
            
    }       
        

private static void printQuestion(Questions_Mull questions) {
       System.out.println(Questions_Mull.question);
        System.out.println(Questions_Mull.answera);
        System.out.println(Questions_Mull.answerb);
        System.out.println(Questions_Mull.answerc);
        System.out.println(Questions_Mull.answerd);
    
}
        

}

Aucun commentaire:

Enregistrer un commentaire