java - Java循环直到满足所有方法
问题描述
我正在做一个简单的基于文本的 OOP java 游戏。我现在有点卡住了,希望能得到帮助
这是代码
while(true){
if(question==1){
String input1 = inputString("You have entered the Dungeon, here you find a " + t.getDesc() + " would you like to fight it? Type Yes/No");
chooseDungeonbattle(input1);
}
if(question == 2){
String input2 = inputString("You have entered the Underworld, it is dark and creepy, but you are not scared...\n SNAP! There is a " + d.getDesc() + " do you wish to fight it?");
chooseUnderworldBattle(input2);
}else if(question == 3){
askUser("You have entered the Dark Forest and you met the Evil Elf, do you wish to fight it? ");
String input3 = inputString("You have entered the Underworld, here you find a " + t.getDesc() + " would you like to fight it? Type Yes/No");
//chooseAbattle3(input3);
}
//If user choose to go to Dungeon
public static void chooseDungeonbattle(String questionInput){
if(questionInput.equals("Yes")||questionInput.equals("yes")){
Battle b = new Battle();
Troll enemy = new Troll(10);
MainPlayer main = new MainPlayer( 100, 100,150,0 );
BadPlayer p = new BadPlayer("",0,0,0);
int goldReceivedDungeon;
b.getOutcome(enemy, main);
if(b.getOutcome(enemy, main).equals("You win")){
System.out.println("You win and receive " + enemy.awardCarried());
goldReceivedDungeon=main.getGold()+ enemy.awardCarried();
System.out.println("You currently have " + enemy.awardCarried());
}else{
System.out.println("Sorry you lost");
}
}
}
public static void chooseUnderworldBattle(String questionInput){
if(questionInput.equals("Yes")||questionInput.equals("yes")){
Battle b = new Battle();
Demon enemy = new Demon(20);
MainPlayer main = new MainPlayer( 100, 100,150,0 );
String asknext;
int goldReceivedDungeon;
b.getOutcome(enemy, main);
if(b.getOutcome(enemy, main).equals("You win")){
System.out.println("You win and receive " + enemy.awardCarried());
goldReceivedDungeon=main.getGold()+ enemy.awardCarried();
asknext = inputString("Would you like to move to you next quest? Press 1 for the Dungeon or 3 for The Dark forest");
}else{
System.out.println("Sorry you lost");
}
}
}
public static void chooseForestBattle(String questionInput){
if(questionInput.equals("Yes")||questionInput.equals("yes")){
Battle b = new Battle();
Elf enemy = new Elf(20);
MainPlayer main = new MainPlayer( 100, 100,150,0 );
int goldReceivedDungeon;
b.getOutcome(enemy, main);
if(b.getOutcome(enemy, main).equals("You win")){
System.out.println("You win and receive " + enemy.awardCarried());
goldReceivedDungeon=main.getGold()+ enemy.awardCarried();
asknext = inputString("Would you like to move to you next quest? Press 1 for the Dungeon or 3 for The Dark forest");
}else{
System.out.println("Sorry you lost");
}
}
}
我要做的是让主角输入他们想先去的地方(地牢,冥界或黑暗森林),如果他们选择先去冥界并在那里获胜,则输入他们想去的地方之后那 - 地牢或黑暗森林。一直如此,直到任务完成,他们从每个任务中收集奖励。我正在尝试使用while循环,但不幸的是循环使用相同的方法:(非常感谢任何帮助:)
解决方案
question变量永远不会被分配一个新值,因此在退出一个“世界”后,您必须使用新值更新question。你的循环是无限的,你需要一个 break 或 return 来退出循环。
推荐阅读
- python-3.x - Python3多处理共享字典由所有进程消耗
- android - 数据绑定和 ColorStateList 选择器
- javascript - 将日期粘贴到顶部,直到在滚动过程中出现新日期,就像在 WhatsApp 中一样
- python - 在python3中将整数划分为“尽可能对称的数组”
- sql - 从表本身设置数据
- rest - API 应该是我的 python 项目中的一个模块还是一个新项目?
- json - 用字符串过滤 JMESPath
- jquery - 多个范围滑块的单一功能
- r - 如何从 R 中非常大的频率表中提取特定频率?
- python - Pandas - 使用正则表达式的条件列引用