如何在Java中从数组中打印出随机字符串

时间:2021-05-28 03:38:14

Hi I have been trying to make my first game for a while but have run into difficulty. I want to pull a randomized String from an array of Strings and then print it out but I have errors in my code. I have looked all over but I can't find the solution. Here is the code: (I have already declared the currentRoom varible)

嗨,我一直试图让我的第一场比赛暂时但是遇到了困难。我想从一个字符串数组中拉出一个随机字符串,然后打印出来,但我的代码中有错误。我看了一遍,但我找不到解决方案。这是代码:(我已经声明了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);

I have an error on my currentRoom variable in line 6 and I think that is messing everything up, but I don't know so the mistake could be anywhere.

我在第6行的currentRoom变量上有一个错误,我认为这是搞乱一切,但我不知道错误可能在任何地方。

Any help is appreciated:)

任何帮助表示赞赏:)

3 个解决方案

#1


1  

Two things you have to change:

你需要改变两件事:

  1. currentRoom should be String Array
  2. currentRoom应该是String Array
  3. Printing string array should be done using Arrays.toString

    应使用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));
    

Sample Output

样本输出

[Goblin Home1, Spider Nest1]

Hope this helps!

希望这可以帮助!

#2


1  

Why do you need a 2D array of strings? I think you can manage with a 1D array instead.

为什么需要2D字符串数组?我认为您可以使用1D阵列进行管理。

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);

#3


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);

#1


1  

Two things you have to change:

你需要改变两件事:

  1. currentRoom should be String Array
  2. currentRoom应该是String Array
  3. Printing string array should be done using Arrays.toString

    应使用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));
    

Sample Output

样本输出

[Goblin Home1, Spider Nest1]

Hope this helps!

希望这可以帮助!

#2


1  

Why do you need a 2D array of strings? I think you can manage with a 1D array instead.

为什么需要2D字符串数组?我认为您可以使用1D阵列进行管理。

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);

#3


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);