java - 迷宫游戏文本输出
问题描述
所以我有这个迷宫游戏,我试图使用 RNG 来确定当用户撞墙、越界或到达迷宫尽头时打印出哪条消息。我的 TextCard 类中有所有这些消息,但我希望所有文本卡,无论它们用于什么,都有一个边框,所以我创建了另一个名为 buildBorder 的方法,但是当我运行我的代码时,它只输出消息而不是边界,我不知道为什么。
构建器方法
public String buildBorder(String cardType){
return ("**********************\n" + cardType + "\n" + "**********************");
墙卡法
public String wallCard(){
Random rnd = new Random();
number = rnd.nextInt(100) + 1;
if(number < 20 && number > 1){
cardText = ("Going that way would lead to a painful face plant");
return (cardText);
}
if(20 < number && number < 40){
cardText = ("Some are destined for greatness, You are destined for a hard surface. You cant go this way");
return (cardText);
}
if(40 < number && number < 60){
cardText = ("Are you lost? or do you just like running into walls?");
return (cardText);
}
if(60 < number && number < 80){
cardText = ("phasing is not your strong suit. Find another way, this wall is as hard as your skull, take the hint.");
return (cardText);
}
else{
cardText = ("You spend longer than you should looking for the door handle, only to realize you ran into a wall.");
return (cardText);
}
}
建设者(部分)
public TextCard(CardType cardType)
{
if(cardType == (CardType.WALL)){
buildBorder(wallCard());
}
获取卡方法
public String getCard(){
return this.cardText;
}
其他类中的方法调用
else if((this.maze.isWall(x, y, "N")) == true){
System.out.println(new TextCard(TextCard.CardType.WALL).getCard());
也忘了发布这个,但这些是我的枚举
public enum CardType{
WALL, OUT, START, END
}
解决方案
当您调用buildBorder(wallCard());
它时,它会返回一个字符串。
您不将此字符串分配给任何东西 - 您不打印它,存储它。所以很自然,什么都不会发生。
String temp = buildBorder(wallCard()); // NOW you can do whatever your heart desires
推荐阅读
- javascript - 如何使用 DataTables 获取行值和删除数据?
- google-app-engine - Stackdriver Trace 与 Google App Engine Go 1.11 运行时
- ios - RxSwift RetryWhen 导致可重入异常
- graphql - 从 commitMutation 的更新程序函数更新存储时出现问题
- python - 如何在网站中使用json数据,我应该使用什么样的数据库?
- logging - 如何在烧瓶视图中使用应用程序日志记录,它返回无法导入名称记录器
- c# - ModelState 验证仅针对少数操作触发
- c++ - 防止常量向量背后的哲学是什么
- python-3.x - 为什么 print(*iterable_list) 以字符串形式返回?
- python - 使用 Pyinstaller 问题创建 Python Tkinter exe 文件