java - 在 if 语句中返回 2 个字符串中的 1 个
问题描述
我正在制作一个小型僵尸生存模拟游戏,并试图添加一些可以增加生存机会的特殊物品。通常,在奔跑中,人类有 10 天的生存时间,并得到一把枪来射击僵尸,如果弹药不足,他们会使用伤害更小但准确性更高的钝器。
在我的个人课程中,有战斗序列方法,我有一个“specialReport”获取方法,我想在“katana”滚动或“ZombieCure”滚动时显示到控制台。
这些项目设置为使用随机数生成器滚动,然后在值为 == 1
第 172-185 行是specialReport()
我想在第 37-38 行的驱动程序类中打印出来的方法。
最初我不得不将zombieCureChance
生成器和katanaDropProb
生成器从第 81 行的 While 循环或 PersonClass 中取出,因为我无法返回它们的值,因为它们在 While 循环的范围内。
但是现在它们在 While 循环之外,我不确定是否会在 DriverClass 的第 28 行的 for 循环中的新一天的每次迭代中重新生成zombieCureChance
和的值。katanaDropProb
目前我没有出现任何错误,但输出不是我需要的。
这是 PersonClass 的代码...
package Zombie;
public class PersonClass {
public static int humanHealth=2;
public static int humanCount = ZombieUtil.START_POEPLE_COUNT;
public static int ammo= 150;
public static int availableFood = ZombieUtil.START_FOOD_COUNT;
//PersonClass Constructor
public PersonClass(int humanHealthIn, int humanCountIn, int ammoIn, int availableFoodIn) {
humanHealth=humanHealthIn;
humanCount=humanCountIn;
ammo=ammoIn;
availableFood=availableFoodIn;
}
public static void setHumanHealth(int humanHealth) {
PersonClass.humanHealth = humanHealth;
}
public static int getHumanCount() {
return humanCount;
}
public static void setHumanCount(int humanCount) {
PersonClass.humanCount = humanCount;
}
public static int getAmmo() {
return ammo;
}
public static void setAmmo(int ammo) {
PersonClass.ammo = ammo;
}
public static int getAvailableFood() {
return availableFood;
}
public static void setAvailableFood(int availableFood) {
PersonClass.availableFood = availableFood;
}
public static int feedHumans(){
PersonClass.availableFood = PersonClass.availableFood - PersonClass.humanCount;
return availableFood;
}
public static int getCurrentHumanCount(){
return humanCount;
}
public static int getAmmoCount(){
return ammo;
}
public static int getHumanHealth(){
return humanHealth;
}
public static int getZombieCureChance(){
//Zombie cure / converter
int zombieCureChance = (int) (Math.random() * (ZombieUtil.ZOMBIECURE_CHANCE + 1));
return zombieCureChance;
}
public static int getKatanaDropChance(){
//katana weapon drop probability
int katanaDropProb = (int) (Math.random() * (ZombieUtil.KATANA_DROP_PROB +1));
return katanaDropProb;
}
public static int fight(){
while (WorldClass.numZombies > 0){
//katana hit probability
int katanaHitProb = (int) (Math.random() * (ZombieUtil.KATANAHIT_PROB +1));
// 1-5 chance hit
int gunHitProb = (int) (Math.random() * (ZombieUtil.GUN_HIT_PROB +1));
//blunt hit probability
int bluntHitProb = (int) (Math.random() * (ZombieUtil.BLUNT_HIT_PROB +1));
// 1-2 death miss prob
int missDeath = (int) (Math.random() * (ZombieUtil.MISS_DEATH_PROB +1));
int gunCritical = (int) (Math.random() * (ZombieUtil.GUN_CRIT_PROB +1));
int bluntCritical = (int) (Math.random() * (ZombieUtil.BLUNT_CRIT_PROB +1));
//Zombiecure
if (getZombieCureChance() == 1){
WorldClass.numZombies--;
PersonClass.humanCount++;
}
//katana drop
if (getKatanaDropChance() == 1){
//katana hits = instant kills
if (katanaHitProb == 1){
WorldClass.numZombies-=2; //kills 2 zombies per slice
}
else{
PersonClass.humanHealth--;
}
}
//Gun attack chance
if (ammo > 0){
if (gunHitProb == 1){
if (gunCritical == 1){
WorldClass.numZombies--;
ammo--;
}
else{
WorldClass.zombieHealth-=2;
ammo--;
}
} else if (missDeath == 1) {
humanCount--;
ammo--;
}
}// end Gun attack
//blunt weapon attack chance
if (ammo <= 0) {
if (bluntHitProb == 1){
if (bluntCritical == 1){
WorldClass.numZombies--;
}
else{
WorldClass.zombieHealth--;
}
}
else{
humanCount--;
}
} // end blunt attack
//human health checker/life updater
if (humanHealth <= 0){
humanCount--;
}
//zombie health checker/life updater
if (WorldClass.zombieHealth <= 0){
WorldClass.numZombies--;
}
}//end while
return humanCount;
}// end fight method
public static String specialReport(){
if (getZombieCureChance() == 1){
String specialReport = String.format("Nice!!! you found a cure vial! \n" +
"You cured one of the enemy zombies and gained an ally ! ");
return specialReport();
}
else if (getKatanaDropChance() == 1){
String specialReport = String.format("Wow!!! you found a katana!! you can now \n'" +
"eliminate 2 zombies per katana attack!!");
return specialReport();
}
else{
return "";
}
}
@Override
public String toString() {
return "\nHumans Health: "+humanHealth+"\n Number of Humans: "+humanCount+"\n " +
"Ammo Value: "+ammo+"\n Available Food: "+availableFood+"\n\n";
} //End PersonClass toString
}// End PersonClass
这是驱动程序类 ZombieDriver ......
package Zombie;
import java.util.Scanner;
public class ZombieDriver {
public static void main(String[] args) {
//WorldClass and PersonClass Object Creation
WorldClass w1 =new WorldClass(3, WorldClass.getNumZombies(), WorldClass.numZombies, WorldClass.days);
PersonClass p1 =new PersonClass(PersonClass.humanHealth, PersonClass.humanCount, PersonClass.ammo, PersonClass.availableFood);
//toString execution
System.out.println(w1);
System.out.println(p1);
Scanner scan = new Scanner( System.in );
System.out.println("Would you like to play a zombie survival simulator?\n Please type [ y ] or [ n ]");
String input = scan.nextLine();
if (input.equals("y")){
// 10 day simulation
for (int i=0; i < ZombieUtil.NUM_DAYS; i++) {
WorldClass.getNumZombies();
int personCount = PersonClass.fight();
PersonClass.feedHumans();
//print out special Items that are found
String specialReport = PersonClass.specialReport();
System.out.printf("%n"+specialReport+"%n");
//daily activity report
String report = WorldClass.dayReport();
System.out.printf("%n"+report+"%n");
//IS THIS AN APPROPRIATE USE OF BREAKING THE LOOP? WITHOUT "BREAK"
if (personCount <= 0) {
i= ZombieUtil.NUM_DAYS;
}
}
if (PersonClass.humanCount > 0 && WorldClass.numZombies <= 0){
System.out.printf("%n%nAwesome you survived 10 days against a horde of " +
"Zombies!!!!!");
}
else if (PersonClass.humanCount <= 0 || PersonClass.availableFood <= 0){
System.out.printf("%n%nIm sorry no one survived the Zombie" +
"onslaught :( ");
}
}
else{
System.out.println("Thank you for playing Zombie Simulator!!");
//close program
}
}
}
我的 Utility 类有这些最终变量,用于调整 Sim 使其可服务......
package Zombie;
public class ZombieUtil {
public static final int NUM_DAYS=10;
public static final int START_FOOD_COUNT = 110;
public static final int START_POEPLE_COUNT =40; //change these where it will be variable to service
public static final int MIN_ZOMBIES=11;
public static final int MAX_ZOMBIES=20;
public static final int ZOMBIECURE_CHANCE=20;
public static final int KATANA_DROP_PROB=15;
public static final int KATANAHIT_PROB=1;
public static final int GUN_HIT_PROB=3;
public static final int GUN_CRIT_PROB = 2;
public static final int BLUNT_HIT_PROB =2;
public static final int BLUNT_CRIT_PROB=10;
public static final int MISS_DEATH_PROB =2;
}
解决方案
我想到了。我返回的是方法而不是变量specialReport
推荐阅读
- smtp - Tcl/Tk 使用 smtp 和 mime 发送电子邮件,附件不显示附加文件名
- python - 为什么我的 twitter 天气机器人应用程序中的列表索引超出范围
- r - 如何将 R^2 值和 p 值添加到每个图表的每个回归线?
- apache-kafka - 使用压缩主题中止 Kafka 事务
- flutter - 从 Firestore 获取文档时如何显示循环进度指示器
- css - 将网格子项设置为默认占用所有列
- jquery - TableSorter — filter_external 上的 select2
- reactjs - 使用 react-table + typescript 自定义表格列
- java - 如何正确配置 Tomcat SSLHostConfig?
- python - 熊猫,删除重复但合并某些列