首页 > 解决方案 > 变量没有相应地更新

问题描述

当我尝试接受用户输入并更新“敏捷”变量时,它只反映在以下几行代码中。当我尝试再次运行相同的命令时,它不会反映以前的输入。由于某种原因,它没有相应地更新变量。

public class SetAttributes {

    public String userInput;
    double Dexterity;
    double Strength;
    double Intelligence;
    double Stamina;
    double SkillPoints = 50;



    public SetAttributes() {
        this.SetDex(0);
        this.SetStr(0);
        this.SetInt(0);
        this.SetSta(0);
        this.SetSkillPoints(50);
    }

    public double GetSP(){
        return SkillPoints;

    }
    public double GetDex() {
        return Dexterity;
    }
    public void SetDex(double dexterity) {
        this.Dexterity = dexterity;
    }

    public double GetStr(){
        return Strength;
    }

    public double GetInt(){
        return Intelligence;
    }

    public double GetSta(){
        return Stamina;
    }





    public void SetStr(double strength){
        this.Strength = strength;
    }
    public void SetInt(double intelligence){
        this.Intelligence = intelligence;
    }
    public void SetSta(double stamina){
        this.Stamina = stamina;
    }
    public void SetSkillPoints(double skillPoints) {this.SkillPoints = skillPoints;};
}


import java.util.Scanner;

public final class PointSpender {


    public static void Spend() {

        SetAttributes A = new SetAttributes();

        System.out.println("Your current stats are ");
        System.out.println("Strength " + A.Strength);
        System.out.println("Stamina " + A.Stamina);
        System.out.println("Intelligence " + A.Intelligence);
        System.out.println("Dexterity " + A.Dexterity);

        System.out.println("Please select the attribute you want to increase. You have " + A.SkillPoints + " available.");

        Scanner Input = new Scanner(System.in);

        A.userInput = Input.nextLine().toLowerCase();

        if(A.userInput.charAt(0) == 'd'){
            System.out.println("Dexterity");
            System.out.println("Your Dexterity is " + A.Dexterity);
            System.out.println("How many points in Dexterity?");



            double Amount = Double.parseDouble(Input.nextLine());

            A.SetDex(A.GetDex() + Amount);
         //   A.setDex(A.Dexterity + Amount);


            System.out.println("You have put " + Amount + " into Dexterity" );
            System.out.println("Your new dexterity is " + A.Dexterity);





        }
    }
}


                          Type "+" to spend your available skillpoints.
                                Type "Logout" to Log Out.
+
Your current stats are 
Strength 0.0
Stamina 0.0
Intelligence 0.0
Dexterity 0.0
Please select the attribute you want to increase. You have 50.0 available.
d
Dexterity
Your Dexterity is 0.0
How many points in Dexterity?
45
You have put 45.0 into Dexterity
Your new dexterity is 45.0
+
Your current stats are 
Strength 0.0
Stamina 0.0
Intelligence 0.0
Dexterity 0.0

标签: java

解决方案


每次调用静态方法“spend”时,您都在创建 SetAttributes 的新实例,然后您将阻止用户输入。但是在创建新实例的块之前,新实例的所有属性都设置为 0。

这发生在您的零参数构造函数中,就在这里:

public SetAttributes() {
        this.SetDex(0);
        this.SetStr(0);
        this.SetInt(0);
        this.SetSta(0);
        this.SetSkillPoints(50);
    }

推荐阅读