返回具有toString方法的二维字符数组(迷宫)

huangapple 未分类评论51阅读模式
英文:

Return 2D array of chars with toString method (maze)

问题

EDIT: 已解决

我在处理一个任务时遇到一些问题。我们需要从文件中读取迷宫(以及解决迷宫,但现在不是问题),但我需要一个toString方法来返回适合打印的迷宫。我无法弄清楚如何正确打印它。
另外,很抱歉所有内容都是挪威语,我们必须使用挪威语。感谢任何帮助!

一些类的背景信息:我有一个名为Labyrint(迷宫)的类,一个抽象类Rute(盒子)。Rute有两个子类,SortRute(黑色盒子)和HvitRute(白色盒子),以及HvitRute的子类Aapning(开口)。黑色盒子被识别为字符'#',白色盒子为'.'。这些盒子类有一个方法char tilTegn(),它返回'#'或'.',如果有用的话。

一个示例迷宫,其中8是列,9是行:

  1. 8 9
  2. #####.###
  3. #.....#.#
  4. #.#####.#
  5. #.#.....#
  6. #.#.###.#
  7. #.#.#.#.#
  8. #.....#.#
  9. #########

谢谢!

英文:

EDIT: SOLVED

I have some trouble with an assignment. We're supposed to read mazes from files (and solve them, but not the issue now), but I need a toString method to return the maze ready for printing. I can't figure out how to print it correctly.
Also, sorry for everything being in norwegian, we have to. Thanks for any help!

Some class context: I have a class Labyrint (maze), an abstract class Rute (box). Rute has two sub classes, SortRute (black box) and HvitRute (white box), and HvitRute's sub class Aapning (opening). The black box is recognized as the char '#', and white '.'. The box classes have a method char tilTegn() which returns either '#' or '.' if that's useful.

An example maze, 8 is columns and 9 is rows:

  1. 8 9
  2. #####.###
  3. #.....#.#
  4. #.#####.#
  5. #.#.....#
  6. #.#.###.#
  7. #.#.#.#.#
  8. #.....#.#
  9. #########

Thanks!

答案1

得分: 0

+ 操作符将内容添加到字符串 s 中,考虑使用 StringBuilder 创建字符串,或使用 String.concat() 方法来连接字符串。我们不能像在原始类型中那样向字符串中添加内容。

s += labyrint[i][j]; 替换为 s.concat(labyrint[i][j];)

英文:

Instead of adding to the String s using the + operator, consider creating a StringBuilder, or use the String.concat() method to concatenate strings. We don't add to Strings like we do in primitive types.

Replace s += labyrint[i][j]; with s.concat(labyrint[i][j];)

huangapple
  • 本文由 发表于 2020年4月11日 00:52:07
  • 转载请务必保留本文链接:https://java.coder-hub.com/61144899.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定