Skip to content

Instantly share code, notes, and snippets.

@jnbutler1815
Created February 24, 2023 13:57
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/507d272485b72ae9c9c73b1f23833930 to your computer and use it in GitHub Desktop.
Save jnbutler1815/507d272485b72ae9c9c73b1f23833930 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 1:
/* Create a simple trivia game
*
* Define (at least) three questions with three answers.
* Prompt the user to type in their answer using the Scanner.
* Then use if/else statements to check if the answer from the
* user is correct. And output that they were.
* If they were wrong, tell the user and output the correct answer.
*
* BONUS: increment a points total each time the user is correct and
* at the end make a unique output for each score the user might have (0 to 3)
*
* IMPORTANT TIPS:
* 1. When reading in Strings from the user use scanner.next() not scanner.nextLine()
* 2. When you have an answer and input that are both Strings you will have to use:
* userInput.equals(answer) instead of userInput == answer.
*/
// StarTrek Trivia
String questionOne = "What was the last name of the captian of the Enterprise?";
String answerOne = "Kirk";
String questionTwo = "What was the name of the bartender on Deep Space Nine?";
String answerTwo = "Quark";
String questionThree = "What was the name of the individual that was created when Tuvok and Neelix were merged in a transportor malfunction?";
String answerThree = "Tuvix";
String userAnswer = "";
int score = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to StarTrek Trivia!");
System.out.println("Get 3 questions correct and you're a SuperFan.");
System.out.println("Get 2 questions correct and you are a fan.");
System.out.println("Get 1 question correct and you are not a fan.");
System.out.println("Here is your first question.");
System.out.println(questionOne);
userAnswer = scanner.next().toUpperCase();
if(userAnswer.equals(answerOne.toUpperCase())) {
System.out.println("Correct, it was " + answerOne);
score++;
} else {
System.out.println("No, I'm sorry the correct answer is: " + answerOne);
}
System.out.println("Here is question two.");
System.out.println(questionTwo);
userAnswer = scanner.next().toUpperCase();
if(userAnswer.equals(answerTwo.toUpperCase())) {
System.out.println("Correct, it was " + answerTwo);
score++;
} else {
System.out.println("No, I'm sorry, the correct answer was: " + answerTwo);
}
System.out.println("Here is the third and final question.");
System.out.println(questionThree);
userAnswer = scanner.next().toUpperCase();
if(userAnswer.equals(answerThree.toUpperCase())) {
System.out.println("Correct, it was: " + answerThree);
score++;
} else {
System.out.println("No, I'm sorry. The correct answer was: " + answerThree);
}
scanner.close();
System.out.println("Tabulating final score...");
switch(score) {
case 1:
System.out.println("You are a not a fan with a score of " + score);
break;
case 2:
System.out.println("You are a fan with a score of " + score);
break;
case 3:
System.out.println("You are a superfan with a score of " + score);
break;
default:
System.out.println("Do you even watch TV? score is " + score);
break;
}
System.out.println("Thanks for playing StarTrek Trivia!");
System.out.println("Bye");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment