Java对象和构造函数/方法
对我来说,解释我试图让这段代码做什么的最好方法是复制和粘贴我所做的作业 我自己编写了代码,但是我遇到的问题是,两个变量似乎根本没有相加,但当我测试代码时,它仍然有效 我想加起来的两个变量是storePower和strength 创建一个类来表示超级英雄。每个超级英雄都有一个名字。每个超级英雄都有自己的长处,但这应该是可选的。如果超级英雄没有力量,他们的默认力量应该是10 每一位超级英雄都可以获得一次力量提升,通过这种方式,他们的力量会得到一定程度的提升 公众级超级英雄{Java对象和构造函数/方法,java,Java,对我来说,解释我试图让这段代码做什么的最好方法是复制和粘贴我所做的作业 我自己编写了代码,但是我遇到的问题是,两个变量似乎根本没有相加,但当我测试代码时,它仍然有效 我想加起来的两个变量是storePower和strength 创建一个类来表示超级英雄。每个超级英雄都有一个名字。每个超级英雄都有自己的长处,但这应该是可选的。如果超级英雄没有力量,他们的默认力量应该是10 每一位超级英雄都可以获得一次力量提升,通过这种方式,他们的力量会得到一定程度的提升 公众级超级英雄{ //private in
//private instance variables declared below.
//name of the super hero
private String name;
//name of power up variable
private int powerUp;
//name of the strength variable
private int strength;
private int storePower;
/*
Power ups work in a way that if the powerUp is greater then or equal to 10 then add 5 to strength
if the powerUp is greater than or equal to 5 then add 2 to the strength
if it is less than 5 then add 1 to the strength.
*/
public void powerUp(int powerUp){
this.powerUp = powerUp;
if(powerUp >= 10){
storePower = 5;
strength = storePower + strength;
//this system out was to test if this bit of code is working.
System.out.println("Power up is set to 10 or higher!" + " The power is: " + strength);
}else if(powerUp >= 5){
storePower = 2;
strength = storePower + strength;
}else if (powerUp < 5){
storePower = 1;
strength = storePower + strength;
}else{
System.out.println("Something is not right..");
}
}
//constructor for if the player wanted to specify the name and the strength
public Superhero(String name, int strength){
this.name = name;
this.strength = strength;
System.out.println(name + " " + strength);
}
//if the user doesn't enter the strength as it is optional
//this constructor below will run and set the default
public Superhero(String name){
this.name = name;
this.strength = 10 + storePower;
System.out.println(name + " " + strength );
}
}
在命令行上运行时的输出为:
独眼巨人10
通电设置为10或更高!功率为:5
我试图获得的输出是15,因为加电设置为高于10,因此if语句将其设置为5,但是我无法将这些加起来修改代码
public void powerUp(int powerUp){
this.powerUp = powerUp;
if(powerUp >= 10){
storePower = 5;
strength = storePower+powerUp
//this system out was to test if this bit of code is working.
System.out.println("Power up is set to 10 or higher!" + " The power is: " + strength );
}else if(powerUp >= 5 & powerUp <10){
storePower = 2;
strength = powerUp+StorePower
System.out.println("Power up is set to 5 or greater but less than 10!" + " The power is: " + strength );
}else if (powerUp < 5){
storePower = 1;
Strength = storePower + powerUp;
System.out.println("Power up is set to less than 5!" + " The power is: " + strength );
}else{
System.out.println("Something is not right..");
}
}
我不能把这些加起来,这不是问题。你为什么不能把它们加起来?您的调试显示了什么?我尝试将这两个变量添加到一起,但没有成功。strength=strength+storePower;将storePower设置为5,然后打印storePower,它为5。您还没有更改该值,为什么还需要其他值?因为storePower被添加到了strength变量中?该代码带来了很多错误,即使在修复错误后也无法使其工作,它仍然将cyclops保持在10。您可以确切地告诉我们您想要什么输出吗,这个代码应该打印出来。通电设置为10或更高!功率是15
public void powerUp(int powerUp){
this.powerUp = powerUp;
if(powerUp >= 10){
storePower = 5;
strength = storePower+powerUp
//this system out was to test if this bit of code is working.
System.out.println("Power up is set to 10 or higher!" + " The power is: " + strength );
}else if(powerUp >= 5 & powerUp <10){
storePower = 2;
strength = powerUp+StorePower
System.out.println("Power up is set to 5 or greater but less than 10!" + " The power is: " + strength );
}else if (powerUp < 5){
storePower = 1;
Strength = storePower + powerUp;
System.out.println("Power up is set to less than 5!" + " The power is: " + strength );
}else{
System.out.println("Something is not right..");
}
}