我试图创建一个计划,该计划是根据Big Five Personality测试指定用户的心理力量。首先,程序创建一个新的类对象和对象的各种属性,如记录结果的得分和五大属性的因素。(以及相关的通灵能力。)保持跟踪。答案来自用户输入,由提示引导。当我运行测试而不是打印五个类别中的每个类别的结果时。它只是一遍又一遍地打印最后一个类别。此外,您可以在任何类别中获得的最高分数为25,最低分为5.然而,不是将分数均匀地分配到每个类别,而是将其全部归为最后一个,最低分为25分,最高分为125分。 / p>
为了澄清这里是我的代码:
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.");
}
}
}
运行代码时,它会打印:
您的预知能力非常出色。 你的预知能力是特殊的。 你的预知能力是特殊的。 你的预知能力是特殊的。 你的预知能力是特殊的。
何时打印:
你的千里眼能力是无足轻重的。 你的Telekinesis能力是微不足道的。 你的额外感官知觉能力是微不足道的。 你的心灵感应能力是微不足道的。 你的预知能力是微不足道的。
(注意上面的例子是在我对所有问题回答A时完成的。 我知道我的代码中存在逻辑错误。但我的问题是......它是什么,以及如何将分数分配给特定的分数元素而不是集中在一个分数元素中。
答案 0 :(得分:1)
您不是将结果存储在数组中,而是每次尝试设置值时访问并覆盖静态字段。有关静态字段的更多信息,您可能需要阅读本文:https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html。
您可以通过将得分,因子和能力声明为非静态成员变量来解决此问题
//...
public int score;
public String factor;
public String ability;
//...
正确初始化数组
//...
BigFivePsychokinesisTest[] ResultsArray = new BigFivePsychokinesisTest[5];
for (int i = 0; i < ResultsArray.length; i++) {
ResultsArray[i] = new BigFivePsychokinesisTest();
}
//...
但是,您应该将这些字段移到单独的类中,并查看使用静态关键字的时间和原因(例如:Java: when to use static methods)。