Java Personality Quiz

Your Task:

  • Pick a theme for your quiz. Example, figure out what character they match from a movie, or what musical instrument fits them.
  • Ask your user at least four questions.
  • Use the questions to determine what category your user goes in.
  • Use int variables to keep track of scores for each category. For example, if you are using Star Wars movie characters, you could create variables called vader and obiwan to record how many responses match those characters.
  • Use if statements to decide which variables to change.
  • Use .equals() to check the user’s answers. In Java you cannot use == to compare strings.
  • Tell the user their result at the end of the quiz.

Example Code:

Creating a Scanner objected named scan and asking two questions. Save each response in a String variable named ans

//import Scanner - goes at top of file
import java.util.Scanner;
//Create a scanner object - only need to do once
Scanner scan = new Scanner(System.in);
// declare answer String variable
String ans;

//print question and get user input using Scanner
System.out.println('Do you use a lightsaber?')
ans = scan.nextLine()

//print next question and get another response
System.out.println('Do you wear a mask?')
ans = scan.nextLine()
//Won't work: answer doesn't get saved in memory
scan.nextLine();

//works better: answer is now saved in ans
ans = scan.nextLine();
//check the user's answers
//in Java, you can't use == to compare strings; use .equals() instead

if (ans.equals("yes")) {
    //add one to vader int variable if yes
    vader += 1
}