从 getter 中减去百分比并显示总计
Deducting a percentage from a getter and displaying total
修复了所有其他问题。目前正在尝试从函数 fightCrime 中的 currentPowerLevel 中扣除 10%。但是,如果没有局部变量,我必须处理用户输入的数字并从中扣除 10%。问题是通常我会有一个带有设定数字的局部变量,我可以简单地重新访问并减去乘以 .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()
手动执行此操作,否则下次您尝试使用 Scanner
它会立即 return 一个空的结果。
powerLevel = kb.nextDouble();
kb.nextLine();
如何递增 getter?
通过获取值、递增它并将新值发送到 setter。
superman.setCrimesStopped(superman.getCrimesStopped() + 10);
superman.setCurrentPowerLevel(superman.getCurrentPowerLevel() - 10);
修复了所有其他问题。目前正在尝试从函数 fightCrime 中的 currentPowerLevel 中扣除 10%。但是,如果没有局部变量,我必须处理用户输入的数字并从中扣除 10%。问题是通常我会有一个带有设定数字的局部变量,我可以简单地重新访问并减去乘以 .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()
手动执行此操作,否则下次您尝试使用 Scanner
它会立即 return 一个空的结果。
powerLevel = kb.nextDouble();
kb.nextLine();
如何递增 getter?
通过获取值、递增它并将新值发送到 setter。
superman.setCrimesStopped(superman.getCrimesStopped() + 10);
superman.setCurrentPowerLevel(superman.getCurrentPowerLevel() - 10);