Skip to content

Instantly share code, notes, and snippets.

@jnbutler1815
Last active February 24, 2023 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnbutler1815/4874cfcbdb4ac1b18dc6a55a7ccb5f22 to your computer and use it in GitHub Desktop.
Save jnbutler1815/4874cfcbdb4ac1b18dc6a55a7ccb5f22 to your computer and use it in GitHub Desktop.
package com.jnbutlerdevelopment;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
/*
* Exercise 2
* a) Re-Create the Trivia Game from before. This time with 5 questions & 5 answers.
* Use arrays to save the question and answers.
* Create at least two methods that make the code more readable and easier to follow.
* --> HINT: Duplicate code can usually be made into methods
* BONUS: Use and endless while loop with if statements so the user can play until they
* don't want to anymore.
*/
// Arrays
String[] questions = new String[5];
String[] answers = new String[5];
// Questions
questions[0] = "Who was the captain of the Enterprise?";
questions[1] = "What was the name of the bartender on Deep Space Nine?";
questions[2] = "What was the name of the individual created when Tuvok and Neelix were merged in a transporter malfunction?";
questions[3] = "Who was the chief engineer on Enterprise X01?";
questions[4] = "What was the number of the ship Voyager?";
// Answers
answers[0] = "Kirk";
answers[1] = "Quark";
answers[2] = "Tuvix";
answers[3] = "Trip";
answers[4] = "NCC-74656";
// Scanner for input
Scanner scanner = new Scanner(System.in);
int score = 0;
int questionNum = 1;
String playAgain = "No";
System.out.println("Welcome to StarTrek Trivia!");
System.out.println();
while(true) {
System.out.println("Here is question " + questionNum);
displayQuestion(questionNum, questions);
score = processUserInput(scanner.next().toUpperCase(), answers, questionNum, score);
questionNum++;
System.out.println("Do want to play again? (Yes or No)");
playAgain = scanner.next();
if(playAgain.toUpperCase().equals("YES")) {
if(questionNum > 5){
System.out.println("Sorry, out of questions!");
break;
}
continue;
} else {
break;
}
}
scanner.close();
System.out.println("Evaluating your score...");
System.out.println("You got " + score + " questions correct!");
// display score and message
switch(score) {
case 5:
System.out.println("You are a Super Fan!");
break;
case 4:
System.out.println("You are an Excellent Fan!");
break;
case 3:
System.out.println("You are a Good Fan!");
break;
case 2:
System.out.println("You are some what of a fan!");
break;
case 1:
System.out.println("You are pathetic!");
break;
default:
System.out.println("Do you even watch TV?");
}
} // end of Main()
// Methods
public static void displayQuestion(int questionNum, String[] questions) {
System.out.println(questions[questionNum -1]);
}
public static void displayAnswer(int answerNum, String[] answers) {
System.out.println("The correct answer is " + answers[answerNum]);
}
public static int processUserInput(String userInput, String[] answers, int answerNum, int score) {
if(userInput.equals(answers[answerNum -1].toUpperCase())) {
System.out.println("Correct!");
score++;
return score;
} else {
System.out.println("Incorrect!");
displayAnswer(answerNum -1, answers);
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment