【问题标题】:I'm trying to add a value to an int in one class and then use it in another, Java我正在尝试向一个类中的 int 添加一个值,然后在另一个类中使用它,Java
【发布时间】:2018-06-20 18:11:39
【问题描述】:

所以正如标题所说,我正在努力为整数添加一个值,然后将它传递给另一个使用它的类,然后这个类会将它传递给下一个类,然后这个类会将它传递给主类。它是一个整数,可以改变我写的小游戏中敌人的统计模板。

我试图在我的两个类中创建构造函数,因为我认为这是问题所在,我试图通过在其中传递一些消息来查看它们是否有效。 问题似乎是,当我在“private int l”中保存一些东西时,它实际上并没有改变那个int的值,我不知道为什么会这样。

这是我的代码,它可能不是很漂亮,所以如果您对我可能想做的结构更改有任何建议,请随时告诉我!

提前致谢!

import java.util.Scanner;

public class Stor {


public static void main(String[] args) {

    Scanner user_Input = new Scanner(System.in);
    Menu user = new Menu();
    EnemyValue monster = new EnemyValue();


    user.namn();
    user.AnvNamn = user_Input.next();

    user.introMeny();
    user.difficulty();
    System.out.println(“Your enemy has " + monster.HP + " HP and " + 
    monster.DMG + " Damage" );

    user_Input.close();
}

}

class Menu {
    Scanner user_Input = new Scanner(System.in);
    String AnvNamn;
    String difficultySvar;
    String nivåSvar;
    int svar;
    private int i; /
    private int l; 
    public int getL() {
    return l;
}
    boolean difficultyLoop = true;
    boolean felLoop = true;

    void introMeny() {
    System.out.println(“Welcome " + AnvNamn + "!");
}

    void namn() {
    System.out.print(“Pick a name: “);
}

    void difficulty() {
    do {
        System.out.println("\nWhat level do you want ?\n1 = Easy.\n2 = 
        Medium.\n3 = Hard.”);   
        svar = user_Input.nextInt();        
    if (svar == 1) {
        System.out.println(“Your not very brave are you ? Are you sure 
        this is how you wanna play ?”);
        difficultySvar = user_Input.next();
        if (difficultySvar.equalsIgnoreCase(“Yes”)) {
            difficultyLoop = false;
            l = 1;
        } // If ja 1
        else if (difficultySvar.equalsIgnoreCase(“Nej”)) {
            System.out.println(“Ahh good! I figuerd you would change 
            your mind.”);
        }
        else 
            System.out.println(“I don’t understand….”);
    } // if 1
    else if (svar == 2) {
        System.out.println(“Not to hard or to easy, a good choice! But 
        maybe you want to go for something harder ? Or maybe easier ?");
        difficultySvar = user_Input.next();
        if (difficultySvar.equalsIgnoreCase(“Yes”)) {
            difficultyLoop = false;
            l = 2;
        } // if ja 2
        else if (difficultySvar.equalsIgnoreCase(“No”)) {
            System.out.println(“I sure hope you don’t pick the easy 
                                 way…..”);
        }
        else 
            System.out.println("I don’t understand….");
    } // Else if 2
    else if (svar == 3) {
        System.out.println(“Damn! We have a big player here! Are you 
        sure you can handle this ?");
        difficultySvar = user_Input.next();
        if (difficultySvar.equalsIgnoreCase(“Yes”)) {
            difficultyLoop = false;
            l = 3;
        } // If ja 3
        else if (difficultySvar.equalsIgnoreCase(“No”)) {
            System.out.println(“Wuss.”);
        }
        else 
            System.out.println(“I don’t understand….”);
    } // Else if 3
    else {
        if (i == 0) {
        System.out.println(“Ha you thought you could fool the system?! 
                            The system fools you!”);
        System.out.println(“Nah but seriously, you can only choose 
                            between 1-3…..“);
        i++;
        } // if i 0
        else if (i == 1) {
            System.out.println(“Alright I get that its hard but 
                                COMEON!”);
            i++;
        } // if i 1
        else if (i == 2) {
            System.out.println(“OKEY YOU GET ONE LAST CHANCE!!”);
            i++;
        } // if i 2
        else if (i == 3) {
            System.out.println(“Alright thats it…. GET OUT!”);
            System.exit(0);
        } // if i 3

    } // Else
    } // do while loop
    while(difficultyLoop == true);

    } //Difficulty metod.
 } // Menu class.

class Nivå {
//Menu level = new Menu();
//int levelChoice = level.getL();
int levelChoice;
private int enemyLife;
public int getenemyLife() {
    return enemyLife;
}
private int enemyDMG;
public int getenemyDMG() {
    return enemyDMG;
}
Nivå(){
    Menu level = new Menu();
    levelChoice = level.getL();
    System.out.println("testNivå");
}
void fiendeLiv() {
    if (levelChoice == 1) 
        enemyLife = 100;
    else if (levelChoice == 2)
        enemyLife = 150;
    else if (levelChoice == 3)
        enemyLife = 200;
} // fiendeliv method

void fiendeDMG() {
    if (levelChoice == 1) 
        enemyDMG = 5;
    else if (levelChoice == 2)
        enemyDMG = 10;
    else if (levelChoice == 3)
        enemyDMG = 15;

} // fiendeDMG method
} // Nivå class

class EnemyValue {
public int HP;
public int DMG;
int maxLife; 
int maxDMG;

EnemyValue(){
    Nivå stats = new Nivå();
    maxLife = stats.getenemyLife();
    maxDMG = stats.getenemyDMG();
    System.out.println("TestEnemyValue");
}

void rank1() {
    HP = maxLife;
    DMG = maxDMG;
} // rank1 easy method

} // EnemyValue class

【问题讨论】:

    标签: java class int


    【解决方案1】:

    您说当您在 l 中保存某些内容时(顺便说一句,变量名的选择不当)它不会保存该值。你怎么知道?在代码的哪里检查值是否被保存?

    在 Nivå 类的构造函数中,您创建一个新菜单,然后在该菜单上调用 getL(),然后再设置该变量的值。

    【讨论】:

      【解决方案2】:

      一切都在您的 public static void main(String[] args) 方法的开头运行,如果它的指令不在其中,则不会运行任何东西。例如,您实际上并没有在 main 方法中创建任何 Niva 对象,因此永远不会调用 Niva 构造函数。这是一个问题。另一个是您的构造函数正在创建对象的新实例,然后获取它们的值;这会为您提供来自全新对象的空值:

      Nivå(){
          Menu level = new Menu(); // Don't do this. This is an empty menu
          levelChoice = level.getL(); // Getting the blank L value from the empty menu
          System.out.println("testNivå");
      }
      

      相反,您需要定义带参数的构造函数来将值传递给类,如下所示:

      Nivå(int level){ // add an int parameter
          levelChoice = level; // Direct assignment
          fiendeDMG(); // Call this in the constructor to set up your last value
          System.out.println("testNivå");
      }
      

      然后,当您调用构造函数时(如果您希望它存在,则必须这样做),包括参数。 Stor 类内部:

      public static void main(String[] args) {
      
          Scanner user_Input = new Scanner(System.in);
          Menu user = new Menu();
      
          user.namn();
          user.AnvNamn = user_Input.next();
      
          user.introMeny();
          user.difficulty(); // Run this before creating the other classes; you need the l value
      
          Nivå niva = new Nivå(user.getL()); // Creates new Niva while also assigning l to the levelChoice and then getting DMG
          EnemyValue monster = new EnemyValue(/*add some parameters for life and dmg*/);
      
      }
      

      还有更多工作要做,比如修改 EnemyLevel 的构造函数。请记住,除非它们连接到从 main 运行的东西并使用函数和构造函数中的参数将数据传递给其他对象,否则永远不会调用方法。希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-06-18
        • 1970-01-01
        • 2019-08-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多