I'm trying to create a program that is a quiz that designates a user's psychic powers based off the Big Five Personality test. First the program creates a new class objects and various attributes of the object such as score for keeping track of results and factor for which attribute of the Big Five.(And correlating psychic ability.) To keep track off. The answers are made from user input guided by prompts. When I run the test instead of it printing the results from each of the five categories. It simply prints the last category over and over again. In addition the highest maximum score you can get in anyone category is 25 and the lowest 5. However instead of distributing the score to each category evenly it lumps it all into the last one for a lowest score of 25 and a highest of 125.
For clarification here is my code:
package com.polaris.quiz;
import java.util.Scanner;
public class BigFivePsychokinesisTest {
public static int questionCount = 1;
public static int score;
public static String factor;
public static String ability;
public int getScore()
{
return score;
}
public String getFactor()
{
return factor;
}
public String getPSIpower()
{
return ability;
}
public static void main(String[] args)
{
QuizStart();
}
public static void QuizStart() {
BigFivePsychokinesisTest[] ResultsArray = new BigFivePsychokinesisTest[5];
ResultsArray[0].factor = "Openness";
ResultsArray[1].factor = "Conscientiousness";
ResultsArray[2].factor = "Extraversion";
ResultsArray[3].factor = "Agreeableness";
ResultsArray[4].factor = "Neuroticism";
ResultsArray[0].ability = "Clarvoyiance";
ResultsArray[1].ability = "Telekinesis";
ResultsArray[2].ability = "Extra Sensory Perception";
ResultsArray[3].ability = "Telepathy";
ResultsArray[4].ability = "Precognition";
ResultsArray[0].score = 0;
ResultsArray[1].score = 0;
ResultsArray[2].score = 0;
ResultsArray[3].score = 0;
ResultsArray[4].score = 0;
System.out.println("Welcome to the discover your ESPER ability test. (This test is based off the Big Five Model of personality");
System.out.println("Please answer the questions honestly.");
System.out.println("You like to learn new things.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("You are a hard worker.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("You are very social with people.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("You get along well with others.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("Your often feel anxious.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("You have an active imagination.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("You get your work done as soon as possible.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("You talk to many people at parties.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("You try to avoid arguments.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("You have many doubts.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("You like to try new things.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("You like to use efficent methods.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("You are eager to speak and act.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("You like it when everyone gets along.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("Your often nervous about your situation in life.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("People sometimes precieve you as impractical and indecisive.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("People sometimes see you as inflexible and narrow minded.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("People sometimes see you as overwhelming and impatient.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("People sometimes see you as weak willed and politically correct.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("People sometimes see you as moody and high strung.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("It annoys you when people aren't open to discussing new ideas or trying new things.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("It annoys you when people waste time and don't take care of their responsibilities.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("It annoys you when people won't engage with you and ignore you.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
System.out.println("It annoys you when people seem insensitive to yours or others feelings.");
answerSelection(ResultsArray);
questionCount = questionCount + 1;
finalResults(ResultsArray);
}
private static void finalResults(BigFivePsychokinesisTest ResultsArray[])
{
int data;
int position;
data = ResultsArray[0].score;
position = 0;
aptitudeFinder(data, position, ResultsArray);
data = ResultsArray[1].score;
position = 1;
aptitudeFinder(data, position, ResultsArray);
data = ResultsArray[2].score;
position = 2;
aptitudeFinder(data, position, ResultsArray);
data = ResultsArray[3].score;
position = 3;
aptitudeFinder(data, position, ResultsArray);
data = ResultsArray[4].score;
position = 4;
aptitudeFinder(data, position, ResultsArray);
}
private static void aptitudeFinder(int data, int position, BigFivePsychokinesisTest ResultsArray[])
{
if(data >= 1 && data <= 5)
{
System.out.println("Your " + ResultsArray[position].ability + " aptitude is Insignificant.");
}
if(data >= 6 && data <= 10)
{
System.out.println("Your " + ResultsArray[position].ability + " aptitude is Low.");
}
if(data >= 11 && data <= 15)
{
System.out.println("Your " + ResultsArray[position].ability + " aptitude is Moderate.");
}
if(data >= 16 && data <= 20)
{
System.out.println("Your " + ResultsArray[position].ability + " aptitude is High.");
}
if(data >= 21 && data <= 25)
{
System.out.println("Your " + ResultsArray[position].ability + " aptitude is Exceptional.");
}
}
private static void answerSelection(BigFivePsychokinesisTest ResultsArray[])
{
Scanner inputDevice = new Scanner(System.in);
System.out.println("A - Almost never true.");
System.out.println("B - Sometimes true.");
System.out.println("C - Moderately true.");
System.out.println("D - Often true.");
System.out.println("E - Almost always true.");
String answer = inputDevice.next();
inputDevice.nextLine();
factorFinder(answer, questionCount, ResultsArray);
}
private static void factorFinder(String answer, int questionCount, BigFivePsychokinesisTest ResultsArray[])
{
if (questionCount == 1 || questionCount == 6 || questionCount == 11 || questionCount == 16 || questionCount == 21)
{
int factor = 0;
scoreFinder(factor, answer, ResultsArray);
}
if (questionCount == 2 || questionCount == 7 || questionCount == 12 || questionCount == 17 || questionCount == 22)
{
int factor = 1;
scoreFinder(factor, answer, ResultsArray);
}
if (questionCount == 3 || questionCount == 8 || questionCount == 13 || questionCount == 18 || questionCount == 23)
{
int factor = 2;
scoreFinder(factor, answer, ResultsArray);
}
if (questionCount == 4 || questionCount == 9 || questionCount == 14 || questionCount == 19 || questionCount == 24)
{
int factor = 3;
scoreFinder(factor, answer, ResultsArray);
}
if (questionCount == 5 || questionCount == 10 || questionCount == 15 || questionCount == 20 || questionCount == 25)
{
int factor = 4;
scoreFinder(factor, answer, ResultsArray);
}
}
private static void scoreFinder(int factor, String answer, BigFivePsychokinesisTest ResultsArray[])
{
switch(answer)
{
case "A":
ResultsArray[factor].score += 1;
break;
case "B":
ResultsArray[factor].score += 2;
break;
case "C":
ResultsArray[factor].score += 3;
break;
case "D":
ResultsArray[factor].score += 4;
break;
case "E":
ResultsArray[factor].score += 5;
break;
default:
System.out.println("Please enter an appropriate response.");
}
}
}
When the code is run, It prints:
Your Precognition aptitude is Exceptional. Your Precognition aptitude is Exceptional. Your Precognition aptitude is Exceptional. Your Precognition aptitude is Exceptional. Your Precognition aptitude is Exceptional.
When is should print:
Your Clairvoyance aptitude is Insignificant. Your Telekinesis aptitude is Insignificant. Your Extra Sensory Perception aptitude is Insignificant. Your Telepathy aptitude is Insignificant. Your Precognition aptitude is Insignificant.
(Note the above example is done when I answer A to all question. I know it's a logical error in my code. But my question is...what is it, and how can I assign a score to a particular score element as oppose to lumped into one.
You're not storing the results in the array, but access and overwrite the static fields each time you try to set a value. For more information on static fields you might want to read this article: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html.
You can fix this by declaring score, factor and ability as non-static member-variables
//...
public int score;
public String factor;
public String ability;
//...
and properly initializing your array
//...
BigFivePsychokinesisTest[] ResultsArray = new BigFivePsychokinesisTest[5];
for (int i = 0; i < ResultsArray.length; i++) {
ResultsArray[i] = new BigFivePsychokinesisTest();
}
//...
However, you should probably move these fields into a seperate class and review when and why you use the static keyword (i.e. here: Java: when to use static methods).