Skip to content

Instantly share code, notes, and snippets.

@andymuncey
Last active July 26, 2022 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save andymuncey/5cb87e69663068daa9fde934128cf5a8 to your computer and use it in GitHub Desktop.
Save andymuncey/5cb87e69663068daa9fde934128cf5a8 to your computer and use it in GitHub Desktop.
True False Quiz example
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Add code to print your name
System.out.println("REPLACE WITH YOUR NAME");
// Add code to print a message with a truth about yourself (try and think of something interesting)
System.out.println("1. I am learning to program in Java");
// Add code to print a message with a lie about yourself
System.out.println("2. I live on the planet Mars");
// Flip a coin, and if you get 'head' modify the code so the lie appears before the truth
// Modify the code so that the first statement printed starts with '1. ' and the second starts with '2. '
// Add another line of code to ask the user to type 1 or 2 depending on which statement they think is correct
System.out.println("Type 1 or 2 to indicate which statement you think is true");
// Add code to read in an integer from the user (using the scanner)
Scanner inputScanner = new Scanner(System.in);
int userChoice = inputScanner.nextInt();
// Use an 'if' statement to check if the user has guessed correctly, and notify them either way
if (userChoice == 1) {
System.out.println("You have guessed correctly");
}
//this is used to check for the wrong answer (later we will learn a better technique than using a second if statement)
if (userChoice == 2){
System.out.println("Sorry your guess is incorrect");
}
//todo: Adapt your programme so it includes two or more lies
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment