gpt4 book ai didi

java - 从 getter 中扣除百分比并显示总计

转载 作者:行者123 更新时间:2023-12-01 10:07:11 25 4
gpt4 key购买 nike

修复了所有其他问题。目前正在尝试在 FightCrime 函数中从 currentPowerLevel 中扣除 10%。但是,如果没有局部变量,我必须处理用户输入的数字并从中扣除 10%。问题是,通常我会有一个带有设定数字的局部变量,我可以简单地重新访问它并减去乘以 0.1 的数字。

package superherodriver;

/**
*
* @author Daniel
*/
public class SuperHero
{
private String name;
private String superPower;
private boolean canFly;
private int crimesStopped;
private final double MAX_POWER_LEVEL;
private double currentPowerLevel;

public SuperHero()
{
MAX_POWER_LEVEL = 100;
name = "";
superPower = "";
canFly = false;
crimesStopped = 0;
currentPowerLevel = 0;

}

//Define non-default constructor
public SuperHero(String aName, String aSuperPower, double aMaxPowerLevel, boolean aCanFly)
{
MAX_POWER_LEVEL = aMaxPowerLevel;
this.name = aName;
this.superPower = aSuperPower;
this.canFly = aCanFly;
this.crimesStopped = 0;
this.currentPowerLevel = MAX_POWER_LEVEL;
}

//Define getters and setters

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSuperPower() {
return superPower;
}

public void setSuperPower(String superPower) {
this.superPower = superPower;
}

public boolean isCanFly() {
return canFly;
}

public void setCanFly(boolean canFly) {
this.canFly = canFly;
}

public int getCrimesStopped() {
return crimesStopped;
}

public void setCrimesStopped(int crimesStopped) {
this.crimesStopped = crimesStopped;
}

public double getCurrentPowerLevel() {
return currentPowerLevel;
}

public void setCurrentPowerLevel(double currentPowerLevel) {
this.currentPowerLevel = currentPowerLevel;
}


}
<小时/>
package superherodriver;
import java.util.Scanner;
import java.util.Random;

/**
*
* @author Daniel
*/

public class SuperHeroDriver {

static Scanner kb = new Scanner(System.in);
public static SuperHero superman;
public static SuperHero wonderwomen;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {

String name, superPower, answer;
double powerLevel;
boolean fly;


System.out.println("What is the name of your favorite super hero?");
name = kb.nextLine();
System.out.println("What is your super hero's power?");
superPower = kb.nextLine();
System.out.println("Can your super hero fly? (yes or no)");

while(true){
answer = kb.nextLine();
if(answer.equalsIgnoreCase("yes")){
fly = true;
break;
} else if(answer.equalsIgnoreCase("no")){
fly = false;
break;
} else {
System.out.println("Sorry, you did not enter an approved response. Please answer yes or no.");
}
}
do{
System.out.println("What is your starting power level? Choose between (0-100)");
powerLevel = kb.nextDouble();
} while(powerLevel < 0 || powerLevel > 100);

kb.nextLine();

// Non-default contructor
superman = new SuperHero(name, superPower, powerLevel, fly);
// Default constructor
wonderwomen = new SuperHero();

System.out.println("What is the name of your favorite super hero?");
wonderwomen.setName(kb.nextLine());
System.out.println("What is your super hero's power?");
wonderwomen.setSuperPower(kb.nextLine());
System.out.println("Can your super hero fly? (yes or no)");

while(true){
answer = kb.nextLine();
if(answer.equalsIgnoreCase("yes")){
wonderwomen.setCanFly(fly = true);
break;
} else if(answer.equalsIgnoreCase("no")){
wonderwomen.setCanFly(fly = false);
break;
} else {
System.out.println("Sorry, you did not enter an approved response. Please answer yes or no.");
}
}

do{
System.out.println("What is your starting power level? Choose between (0-100)");
wonderwomen.setCurrentPowerLevel(kb.nextDouble());
} while(wonderwomen.getCurrentPowerLevel() < 0 || wonderwomen.getCurrentPowerLevel() > 100);

do{
fightCrime(superman, wonderwomen);
System.out.println("Superman's current power level is " + superman.getCurrentPowerLevel() + " and stopped " + superman.getCrimesStopped() + " crimes.");
System.out.println("Wonder woman's current power level is " + wonderwomen.getCurrentPowerLevel() + " and stopped " + wonderwomen.getCrimesStopped() + " crimes.");

}while(superman.getCurrentPowerLevel() > 0 && superman.getCrimesStopped() < 10);

}

public static void fightCrime(SuperHero superman, SuperHero wonderwomen){
Random randomNumber = new Random();
int ranNum1, ranNum2;
ranNum1 = randomNumber.nextInt(11) + 1;
ranNum2 = randomNumber.nextInt(11) + 1;
// Stop crime if random number is Even, lose power if odd
if(ranNum1 % 2 == 0){
System.out.println("Crime was stopped!");
superman.setCrimesStopped(superman.getCrimesStopped() + 1);
}
else{
System.out.println("My powers are getting weaker!");
superman.setCurrentPowerLevel(superman.getCurrentPowerLevel() - 10);
}

if(ranNum2 % 2 == 0){
System.out.println("Crime was stopped!");
wonderwomen.setCrimesStopped(wonderwomen.getCrimesStopped() + 1);
}
else{
System.out.println("My powers are getting weaker!");
wonderwomen.setCurrentPowerLevel(wonderwomen.getCurrentPowerLevel() - 10);
}

}

}

最佳答案

如何防止提示被跳过?

由于 Scanner.nextDouble() 不会使用用户按 Enter 键时生成的换行符,因此您需要使用 Scanner.nextLine() 手动执行此操作,否则下次您尝试使用该扫描仪读取时,它将立即返回空结果。

powerLevel = kb.nextDouble();
kb.nextLine();

如何增加 getter?

通过获取值,递增它,并将新值发送到 setter 。

superman.setCrimesStopped(superman.getCrimesStopped() + 10);
superman.setCurrentPowerLevel(superman.getCurrentPowerLevel() - 10);

关于java - 从 getter 中扣除百分比并显示总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36366200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com