java - 变量没有相应地更新
问题描述
当我尝试接受用户输入并更新“敏捷”变量时,它只反映在以下几行代码中。当我尝试再次运行相同的命令时,它不会反映以前的输入。由于某种原因,它没有相应地更新变量。
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
解决方案
每次调用静态方法“spend”时,您都在创建 SetAttributes 的新实例,然后您将阻止用户输入。但是在创建新实例的块之前,新实例的所有属性都设置为 0。
这发生在您的零参数构造函数中,就在这里:
public SetAttributes() {
this.SetDex(0);
this.SetStr(0);
this.SetInt(0);
this.SetSta(0);
this.SetSkillPoints(50);
}
推荐阅读
- javascript - AngularJS,谷歌浏览器阻止我的应用程序发出发布请求,需要帮助添加标题
- java - 在 TinyMCE 中为流 Vaadin 组件使用自定义皮肤时遇到问题
- spring-boot - 收到错误 java.nio.file.AccessDeniedException
- c - 如何通过套接字连接从命令行传递字符串
- javascript - 减少 React 表单验证中的 if 数量
- powershell - 所有 AD 用户的 Powershell 删除
- amazon-web-services - Terraform 错误:运行 terraform init 时模块的无关标签
- jquery - require.js 和 jquery UI 小部件的淘汰赛可排序问题
- azure - Azure:从应用服务访问 Key Vault 时,如何解决“策略要求调用者'...'使用代表 (OBO) 流”?
- mysql - MySQL:将单行显示为列(简单地说,通过 SELECT)