我正在制作一个RPG类构建器,它根据用户选择的项目传递stat值,根据该选择创建一个新的类实例,但是当我显示值时,就好像值是从不的过去了。
这是我的代码首先是Class页面,然后是一个要测试的文件并设置Class值:
package com.polaris.mbti;
public class MBTI_Type
{
private int Se;
private int Si;
private int Ne;
private int Ni;
private int Te;
private int Ti;
private int Fe;
private int Fi;
private int Vitality;
private int Durability;
private int Strength;
private int Tact;
private int Loyalty;
private int Friendliness;
private int Ambition;
private int Insight;
private int Foresight;
private int Humanity;
private int Revelation;
private int Charisma;
private int Dexterity;
private int Evasion;
private int Accuracy;
private int Agility;
private int Zest;
private int Grace;
private int Intelligence;
private int Curiousity;
private int Analysis;
private int Idealism;
private int Enthusiasm;
private int Conviction;
public void setBaseStats(int introSen, int extroSen, int introThk, int extroThk, int introInuit, int extroInuit, int introFel, int extroFel)
{
Se = extroSen;
Si = introSen;
Ne = extroInuit;
Ni = introInuit;
Te = extroThk;
Ti = introThk;
Fe = extroFel;
Fi = introFel;
setMajorStats();
}
public void setMajorStats()
{
setMinorStats(Te, Si, Vitality, Durability, Strength);
setMinorStats(Te, Ni, Ambition, Insight, Foresight);
setMinorStats(Fe, Si, Tact, Loyalty, Friendliness);
setMinorStats(Fe, Ni, Humanity, Revelation, Charisma);
setMinorStats(Ti, Se, Dexterity, Evasion, Accuracy);
setMinorStats(Ti, Ne, Intelligence, Curiousity, Analysis);
setMinorStats(Fi, Se, Agility, Zest, Grace);
setMinorStats(Fi, Ne, Idealism, Enthusiasm, Conviction);
}
public void setMinorStats(double A, double B, double Major, double MinorA, double MinorB)
{
Major = (A + B)/2;
if(A > B)
{
MinorA = (Major) - 1;
MinorB = (Major) + 1;
}
if(B > A)
{
MinorA = (Major) + 1;
MinorB = (Major) - 1;
}
}
public void levelUp()
{
}
public void showStats()
{
System.out.println(Vitality);
System.out.println(Durability);
System.out.println(Strength);
System.out.println(Tact);
System.out.println(Loyalty);
System.out.println(Friendliness);
System.out.println(Ambition);
System.out.println(Insight);
System.out.println(Foresight);
System.out.println(Humanity);
System.out.println(Revelation);
System.out.println(Charisma);
System.out.println(Dexterity);
System.out.println(Evasion);
System.out.println(Accuracy);
System.out.println(Agility);
System.out.println(Zest);
System.out.println(Grace);
System.out.println(Intelligence);
System.out.println(Curiousity);
System.out.println(Analysis);
System.out.println(Idealism);
System.out.println(Enthusiasm);
System.out.println(Conviction);
}
}
以下是具有测试主要方法的类:
package com.polaris.mbti;
import java.util.Scanner;
public class TestMBTI2
{
public static void main(String[] args)
{
Scanner inputDevice = new Scanner(System.in);
System.out.println("Please enter a personality type from the choices:");
System.out.println(" 1 - ESTJ");
System.out.println(" 2 - ENTJ");
System.out.println(" 3 - ISTP");
System.out.println(" 4 - INTP");
System.out.println(" 5 - ESFJ");
System.out.println(" 6 - ENFJ");
System.out.println(" 7 - ISFP");
System.out.println(" 8 - INFP");
System.out.println(" 9 - ESTP");
System.out.println(" 10 - ESFP");
System.out.println(" 11 - ISTJ");
System.out.println(" 12 - ISFJ");
System.out.println(" 13 - ENTP");
System.out.println(" 14 - ENFP");
System.out.println(" 15 - INTJ");
System.out.println(" 16 - INFJ");
int choice = inputDevice.nextInt();
switch(choice)
{
case 1:
MBTI_Type ESTJ = new MBTI_Type();
ESTJ.setBaseStats(14, 6, 8, 16, 4, 12, 10, 2);
ESTJ.showStats();
break;
case 2:
MBTI_Type ENTJ = new MBTI_Type();
ENTJ.setBaseStats(4, 12, 8, 16, 14, 6, 10, 2);
ENTJ.showStats();
break;
case 3:
MBTI_Type ISTP = new MBTI_Type();
ISTP.setBaseStats(6, 14, 16, 8, 12, 4, 2, 10);
ISTP.showStats();
break;
case 4:
MBTI_Type INTP = new MBTI_Type();
INTP.setBaseStats(12, 4, 16, 8, 4, 12, 2, 10);
INTP.showStats();
break;
case 5:
MBTI_Type ESFJ = new MBTI_Type();
ESFJ.setBaseStats(14, 6, 10, 2, 4, 12, 8, 16);
ESFJ.showStats();
break;
case 6:
MBTI_Type ENFJ = new MBTI_Type();
ENFJ.setBaseStats(4, 12, 10, 2, 14, 6, 16, 8);
ENFJ.showStats();
break;
case 7:
MBTI_Type ISFP = new MBTI_Type();
ISFP.setBaseStats(6, 14, 2, 10, 12, 4, 16, 8);
ISFP.showStats();
break;
case 8:
MBTI_Type INFP = new MBTI_Type();
INFP.setBaseStats(12, 4, 2, 10, 6, 14, 16, 8);
INFP.showStats();
break;
case 9:
MBTI_Type ESTP = new MBTI_Type();
ESTP.setBaseStats(8, 16, 14, 6, 10, 2, 4, 12);
ESTP.showStats();
break;
case 10:
MBTI_Type ESFP = new MBTI_Type();
ESFP.setBaseStats(8, 16, 4, 12, 10, 2, 14, 6);
ESFP.showStats();
break;
case 11:
MBTI_Type ISTJ = new MBTI_Type();
ISTJ.setBaseStats(16, 8, 6, 14, 2, 10, 12, 4);
ISTJ.showStats();
break;
case 12:
MBTI_Type ISFJ = new MBTI_Type();
ISFJ.setBaseStats(16, 8, 12, 4, 2, 10, 6, 14);
ISFJ.showStats();
break;
case 13:
MBTI_Type ENTP = new MBTI_Type();
ENTP.setBaseStats(10, 2, 14, 6, 8, 16, 4, 12);
ENTP.showStats();
break;
case 14:
MBTI_Type ENFP = new MBTI_Type();
ENFP.setBaseStats(10, 2, 4, 12, 8, 16, 14, 6);
ENFP.showStats();
break;
case 15:
MBTI_Type INTJ = new MBTI_Type();
INTJ.setBaseStats(2, 10, 6, 14, 16, 8, 12, 4);
INTJ.showStats();
break;
case 16:
MBTI_Type INFJ = new MBTI_Type();
INFJ.setBaseStats(2, 10, 12, 4, 16, 8, 14, 6);
INFJ.showStats();
break;
}
}
}
答案 0 :(得分:1)
问题似乎与setMinorStats
方法有关。从实施来看,它似乎在计算一些价值。但是,这些值未设置为对象(即没有this.something = value
分配)。
该方法修改了传递的参数值,但由于Java为pass by value
,因此无法更改实际值。我建议您查看these示例。
答案 1 :(得分:0)
作为调试时的一般原则,坚持使用默认值并查看是否出现,然后您就会知道您的程序是否完全没有进入交换机,或者只是没有识别出您拥有的值。
答案 2 :(得分:0)
问题是你从未真正设置过你正在打印的任何属性(活力,耐久性等)。
你的意思是在setMinorStats()中设置它们吗?
在setMinorStats()方法的if语句中,为此效果添加一些内容
this.Vitality = Major;
如果你用下面的方法替换你的方法,并且在第一种情况下用键盘输入1运行,你会看到Vitality在输出时设置为8。
public void setMinorStats(double A, double B, double Major, double MinorA, double MinorB)
{
Major = (A + B)/2;
if(A > B)
{
MinorA = (Major) - 1;
MinorB = (Major) + 1;
/*
* Here is what you need to do-- set your fields based on your logic
*/
this.Vitality = (int)Major;
}
if(B > A)
{
MinorA = (Major) + 1;
MinorB = (Major) - 1;
}
}
答案 3 :(得分:0)
这是我的问题的解决方案,我只是将我的方法分成另外三个,并使它们具有返回语句,如下所示。
public void setBaseStats(int introSen, int extroSen, int introThk, int extroThk, int introInuit, int extroInuit, int introFel, int extroFel)
{
Se = extroSen;
Si = introSen;
Ne = extroInuit;
Ni = introInuit;
Te = extroThk;
Ti = introThk;
Fe = extroFel;
Fi = introFel;
setMajorStats();
}
public void setMajorStats()
{
Vitality = setMinorStats(Te, Si);
Durability = setMinorStatsA(Vitality, Te, Si);
Strength = setMinorStatsB(Vitality, Te, Si);
Ambition = setMinorStats(Te, Ni);
Insight = setMinorStatsA(Ambition, Te, Ni);
Foresight = setMinorStatsB(Ambition, Te, Ni);
Tact = setMinorStats(Fe, Si);
Loyalty = setMinorStatsA(Tact, Fe, Si);
Friendliness = setMinorStatsB(Tact, Fe, Si);
Humanity = setMinorStats(Fe, Ni);
Revelation = setMinorStatsA(Humanity, Fe, Ni);
Charisma = setMinorStatsB(Humanity, Fe, Ni);
Dexterity = setMinorStats(Ti, Se);
Evasion = setMinorStatsA(Dexterity, Ti, Se);
Accuracy = setMinorStatsB(Dexterity, Ti, Se);
Intelligence = setMinorStats(Ti, Ne);
Curiousity = setMinorStatsA(Intelligence, Ti, Ne);
Analysis =setMinorStatsB(Intelligence, Ti, Ne);
Agility = setMinorStats(Fi, Se);
Zest = setMinorStatsA(Agility, Fi, Se);
Grace = setMinorStatsB(Agility, Fi, Se);
Idealism = setMinorStats(Fi, Ne);
Enthusiasm = setMinorStatsA(Idealism, Fi, Ne);
Conviction = setMinorStatsB(Idealism, Fi, Ne);
}
public int setMinorStats(int A, int B)
{
int Major = (A + B)/2;
return Major;
}
public int setMinorStatsA(int Major, int A, int B)
{
int MinorA = 0;
if(A > B)
MinorA = (Major) - 1;
if(B > A)
MinorA = (Major) + 1;
return MinorA;
}
public int setMinorStatsB(int Major, int A, int B)
{
int MinorB = 0;
if(A > B)
MinorB = (Major) + 1;
if(B > A)
MinorB = (Major) - 1;
return MinorB;
}