java—初始化类时未传递值

6qftjkof  于 2021-07-13  发布在  Java
关注(0)|答案(4)|浏览(414)

我正在制作一个rpg类生成器,它根据用户选择的项传递stat值,根据这个选择,会生成一个新的类示例,但是当我显示值时,就好像从未传递过值一样。
下面是我的代码,首先是类页面,然后是一个用于测试和设置类值的文件:

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;
    }

}

}
uujelgoq

uujelgoq1#

问题似乎出在 setMinorStats 方法。从实现来看,它似乎在计算一些值。但是,这些值没有设置到对象(即没有 this.something = value 工作分配)。
该方法修改了传递的参数的值,但是不会因为java的存在而改变实际值 pass by value . 我建议看一下这些例子。

ut6juiuv

ut6juiuv2#

作为调试时的一般原则,请输入一个默认值,然后查看是否出现该值,这样您就知道您的程序是否根本没有到达开关,或者只是无法识别任何值(如果有)。

yjghlzjz

yjghlzjz3#

问题是,你从来没有真正设置任何属性,你打印(生命力,耐用性等)。
你的意思是在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;
      }
  }
sulc1iza

sulc1iza4#

这是我的问题的一个解决方案,我简单地将我的方法分为三个其他方法,并使它们具有返回语句,如下所示。

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;
}

相关问题