如何在基于文本的Java游戏中使用while循环?
首先是一些文本,展示了我对代码工作方式的思考过程。我对编程非常陌生,几天前才开始学习java。我想我对oop的工作原理有了基本的了解,但我不能在代码中很好地实现它。虽然while循环的使用对我来说有点意义,但我只是从它们开始;D 我想要的东西: 有3个选项的主菜单 菜单(营地)如何在基于文本的Java游戏中使用while循环?,java,loops,while-loop,logic,game-engine,Java,Loops,While Loop,Logic,Game Engine,首先是一些文本,展示了我对代码工作方式的思考过程。我对编程非常陌生,几天前才开始学习java。我想我对oop的工作原理有了基本的了解,但我不能在代码中很好地实现它。虽然while循环的使用对我来说有点意义,但我只是从它们开始;D 我想要的东西: 有3个选项的主菜单 菜单(营地) 任务(激活任务循环) 进入城镇(激活城镇循环) 退出游戏(退出程序) 探索 与随机怪物战斗并获得金币/分数的游戏循环 “怪物出现了” 战斗怪物 对怪物造成伤害吗 怪物会对背部造成伤害 返回主回路 使用项 选择项目 使用项
import java.util.Scanner;
public class logic
{
public static void main(String[] args)
{
boolean running = true;
Scanner in = new Scanner(System.in);
System.out.println("This is a test");
while(running)
{
boolean camp = true;
boolean dungeon = false;
boolean town = false;
String input = in.nextLine();
while(camp)
{
System.out.println("what u do?");
System.out.println("1. go dungeon");
System.out.println("2. go town");
System.out.println("3. quit");
if (input.equals("1"))
{
dungeon = true;
while(dungeon)
{
System.out.println("you are in the dungeon");
dungeon = false;
break;
}
}
else if (input.equals("2"))
{
dungeon = false;
town = true;
while(town)
{
System.out.println("you are in the town");
town = false;
break;
}
}
else if (input.equals("3"))
{
break;
}
else
{
System.out.println("invalid command!");
}
}
}
}
}
就我个人而言,我不会使用那么多while循环,而是将每个部分的逻辑放在自己的函数中,然后使用if-else-if/switch语句来决定应该调用哪个函数,并在while循环中执行此操作,遗憾的是,我无法测试这一点,因为我没有;我没有Java环境,我已经有一段时间没有使用Java了 这是我想出的新代码。我能够想出如何将所有不同的领域分解成各自的方法。这将使构建游戏世界变得更加容易:D
import java.util.Scanner;
public class LogicTest
{
Scanner in = new Scanner(System.in);
public static void main(String[] args)
{
LogicTest game;
game = new LogicTest(); // a new instance of the public class folder has to be created in order to use all the other methods
game.run();
}
public void run() // this method is used to run the game from the main method
{
camp();
}
public void quest()
{
System.out.println("you go on quest");
camp();
}
public void town()
{
System.out.println("you go to town");
camp();
}
public void camp() // this method acts as the game hub where the player can choose what they want to do next
{
System.out.println("you are in your camp");
System.out.println("----------------------");
System.out.println("what do you want to do?");
System.out.println("1. go quest");
System.out.println("2. go town");
System.out.println("3. quit");
String input = in.nextLine();
// each one of these options just calls its respective method
// the methods are what have the menus and everything for each location
if (input.equals("1"))
{
quest();
}
else if (input.equals("2"))
{
town();
}
else if(input.equals("3")) // by leaving this empty the program has nothing else to read with this option and so it closes
{
}
else
{
camp();
}
}
}
你可能需要给营地分配false,然后在某处跑步。如果我错了,请纠正我,但是,我无法在地牢或城镇内这样做,因为他们在营地内,并运行循环。你可能想在选项3中做些什么,好吧,所以我在选项3中添加了两行,声明营地和运行都是假的,但这不起作用。然后我试着将camp声明为false,但也没有这样做……另一件让我感到奇怪的事情是,当我运行程序时,唯一显示的是“这是一个测试”打印。我认为running和camp循环会自动显示,因为它们都被声明为true…但它们不是。如果您输入任何类型的输入,这就是无限循环开始发生的时候,我最终需要关闭命令prompit,在该行等待输入:String input=in.nextLine();好吧,是的,我一定会考虑的。我同意while循环是松散的。就像我说的,虽然创建函数/对象并调用它们对我来说仍然有点奇怪,我仍然在学习如何做到这一切。哦,是的,我只是在使用命令提示符和记事本++来完成一切。如果你有时间测试代码,这是一个简单的方法,我会非常感激:)所以,弄明白你的建议其实并不像我最初想的那么难…我得到了一个正在工作的原型…谢谢你!:DGlad要听,您可能应该在此处发布您的解决方案,以便将此问题标记为已解决/已回答。