嗨,我一直试图让我的第一场比赛暂时但是遇到了困难。我想从一个字符串数组中拉出一个随机字符串,然后打印出来,但我的代码中有错误。我已经看了一遍,但我找不到解决方案。这是代码: (我已经声明了currentRoom变量)
int currentRoom;
String [][] rooms = {{"Start", "Treasure Room1"}, {"Goblin Home1", "Spider Nest1"}};
Random rand = new Random()
currentRoom = rooms [rand.nextInt( rooms.length)];
System.out.println(currentRoom);
我在第6行的currentRoom变量上有一个错误,我认为这会弄乱一切,但我不知道错误可能在任何地方。
感谢任何帮助:)
答案 0 :(得分:1)
你必须改变两件事:
打印字符串数组应使用Arrays.toString
完成 String[] currentRoom;
String [][] rooms = {{"Start", "Treasure Room1"}, {"Goblin Home1", "Spider Nest1"}};
Random rand = new Random();
currentRoom = rooms [rand.nextInt(rooms.length)];
System.out.println(Arrays.toString(currentRoom));
示例输出
[Goblin Home1, Spider Nest1]
希望这有帮助!
答案 1 :(得分:1)
为什么需要2D数组字符串? 我认为你可以用一维数组来管理。
String currentRoom;
String[] rooms = {"Start", "Treasure Room1", "Goblin Home1", "Spider Nest1"};
Random rand = new Random();
currentRoom = rooms [rand.nextInt( rooms.length)];
System.out.println(currentRoom);
答案 2 :(得分:0)
String currentRoom;
String [][] rooms = {{"Start", "Treasure Room1"}, {"Goblin Home1", "Spider Nest1"}};
Random rand = new Random();
int rnd = rand.nextInt( rooms.length);
currentRoom = rooms [rnd][0] + ":"+ rooms [rnd][1];
System.out.println(currentRoom);