随机从随机字符串列表中选择(JAVA)

huangapple go评论116阅读模式
英文:

Random Selection from Random List of Strings (JAVA)

问题

我有非常有限的编程经验,正在尝试为我和我的朋友在《使命召唤:战区》中创建一个随机职业生成器,只是为了好玩。这是一个非常基本的生成器,但我遇到了一些问题,无法得到我想要的输出。它变成了一个比我预期的项目更大的项目,所以我会接受任何帮助!

对于那些没有玩过这个游戏的人来说,最多可以选择9种不同的附件类别(如消音器、枪管、光学镜、枪托等),但只能装备最多5个,这意味着某些附件类别将不会被使用。由于我正在尝试先解决基本问题,所以我还没有添加所有的附件类别或特殊附件。

我希望最终的输出类似于:

  1. 主武器M4A1
  2. 主武器握把Commando
  3. 主武器激光
  4. 主武器枪托无枪托附件
  5. 主武器弹夹50
  6. 主武器后握把Stippled
  7. 主武器枪管大枪管

目前,它只会输出类似于:

  1. 主武器M4A1 主要附件Commando 无枪托附件 50 Stippled

这是我目前的代码(如果代码看起来不好,请谅解lol):

  1. import java.util.concurrent.ThreadLocalRandom;
  2. public class RandomClass {
  3. public static void main(String[] args) {
  4. System.out.println("主武器:" + randomPrimaryGun());
  5. System.out.println("主要附件:" + primaryAttachments());
  6. System.out.println("副武器:" + randomSecondaryGun());
  7. }
  8. public static String randomPrimaryGun() {
  9. String[] primaryGuns = {"M4A1", "Kilo", "Grau", "Bruen", "FAL", "M13"};
  10. int randIdx = ThreadLocalRandom.current().nextInt(primaryGuns.length);
  11. String randPrimary = primaryGuns[randIdx];
  12. return randPrimary;
  13. }
  14. public static String randomSecondaryGun() {
  15. String[] secondaryGuns = {"MP5", "P90", "AUG", "Uzi", "MP7"};
  16. int randIdx = ThreadLocalRandom.current().nextInt(secondaryGuns.length);
  17. String randSecondary = secondaryGuns[randIdx];
  18. return randSecondary;
  19. }
  20. public static String primaryAttachments() {
  21. String[] primaryGrip = {"Merc", "Ranger", "Commando", "None"};
  22. String[] primaryLaser = {"Tac", "5mw", "None"};
  23. String[] primaryStock = {"无枪托附件", "无枪托", "快速枪托"};
  24. String[] primaryMag = {"50发", "60发"};
  25. String[] primaryRearGrip = {"Stippled", "None"};
  26. String[] primaryBarrel = {"大枪管", "短枪管"};
  27. int randIdx0 = ThreadLocalRandom.current().nextInt(primaryGrip.length);
  28. int randIdx1 = ThreadLocalRandom.current().nextInt(primaryLaser.length);
  29. int randIdx2 = ThreadLocalRandom.current().nextInt(primaryStock.length);
  30. int randIdx3 = ThreadLocalRandom.current().nextInt(primaryMag.length);
  31. int randIdx4 = ThreadLocalRandom.current().nextInt(primaryRearGrip.length);
  32. int randIdx5 = ThreadLocalRandom.current().nextInt(primaryBarrel.length);
  33. String grip = primaryGrip[randIdx0];
  34. String laser = primaryLaser[randIdx1];
  35. String stock = primaryStock[randIdx2];
  36. String mag = primaryMag[randIdx3];
  37. String rearGrip = primaryRearGrip[randIdx4];
  38. String barrel = primaryBarrel[randIdx5];
  39. // 在此处构建你的输出字符串
  40. }
  41. }

我知道我需要一些if/else语句,也许需要一些循环,但我不知道如何在从列表中随机选择的情况下实现它。如果有人能帮我入门,我将不胜感激!一旦我知道了基本方法,我就可以从那里开始,并添加我需要的任何其他内容!

我还没有开始处理副武器附件,因为我无法弄清楚如何处理主要附件,但实质上它会是相同的代码,只是有一些不同。

谢谢!

英文:

I have very minimal experience programming and I'm trying to create a random(ish) class generator for my buddies and I on Warzone for fun. It's a very basic generator but I'm having some trouble figuring out how to get the outputs I want. It turned into a bigger project than I anticipated so I'll take any help I can get!

For anyone who hasn't played the game, there are at most 9 different classes of attachments you can choose from (i.e. suppressors, barrels, optics, stocks, etc) but you can only have a maximum of 5 equipped, meaning some attachment classes won't be used. I haven't added all the attachment categories or special attachments there might be since I'm trying to work out the basics first.

I'd like to have the final output be something like:

  1. Primary Gun: M4A1
  2. Primary Grip: Commando
  3. Primary Laser: None
  4. Primary Stock: No Stock Attachment
  5. Primary Mag: 50
  6. Primary Rear Grip: Stippled
  7. Primary Barrel: Big Barrel

As of now it just outputs something like

  1. Primary Gun: M4A1 Primary Attachments: Commando None No Stock Attachment 50 Stippled None

This is what I have so far (excuse if the code looks ugly lol)

  1. import java.util.concurrent.ThreadLocalRandom;
  2. public class RandomClass {
  3. public static void main(String[] args) {
  4. System.out.println("Primary: " + randomPrimaryGun());
  5. System.out.println("Attachments: " + primaryAttachments());
  6. System.out.println("Secondary: " + randomSecondaryGun());
  7. }
  8. public static String randomPrimaryGun() {
  9. String[] primaryGuns = {"M4A1", "Kilo", "Grau", "Bruen", "FAL", "M13"};
  10. int randIdx = ThreadLocalRandom.current().nextInt(primaryGuns.length);
  11. String randPrimary = primaryGuns[randIdx];
  12. return randPrimary;
  13. }
  14. public static String randomSecondaryGun() {
  15. String[] secondaryGuns = {"MP5", "P90", "AUG", "Uzi", "MP7"};
  16. int randIdx = ThreadLocalRandom.current().nextInt(secondaryGuns.length);
  17. String randSecondary = secondaryGuns[randIdx];
  18. return randSecondary;
  19. }
  20. public static String primaryAttachments() {
  21. String[] primaryGrip = {"Merc ", "Ranger ", "Commando ", "None "};
  22. String[] primaryLaser = {"Tac ", "5mw ", "None "};
  23. String[] primaryStock = {"No Stock Attachment ", "No Stock ", "Fast stock "};
  24. String[] primaryMag = {"50 ", "60 "};
  25. String[] primaryRearGrip = {"Stippled ", "None "};
  26. String[] primaryBarrel = {"Big Barrel", "Short Barrel"};
  27. int randIdx0 = ThreadLocalRandom.current().nextInt(primaryGrip.length);
  28. int randIdx1 = ThreadLocalRandom.current().nextInt(primaryLaser.length);
  29. int randIdx2 = ThreadLocalRandom.current().nextInt(primaryStock.length);
  30. int randIdx3 = ThreadLocalRandom.current().nextInt(primaryMag.length);
  31. int randIdx4 = ThreadLocalRandom.current().nextInt(primaryRearGrip.length);
  32. int randIdx5 = ThreadLocalRandom.current().nextInt(primaryBarrel.length);
  33. String grip = primaryGrip[randIdx0];
  34. String laser = primaryLaser[randIdx1];
  35. String stock = primaryStock[randIdx2];
  36. String mag = primaryMag[randIdx3];
  37. String rearGrip = primaryRearGrip[randIdx4];
  38. String barell = primaryBarrel[randIdx5];
  39. }
  40. }

I know I need some if/else statements and maybe some loops but I have no idea how to implement that with a random selection from a list. If anyone could at least help me get started I'd really appreciate it! Once I know the basic method I can go from there and add whatever else I need to!

I haven't bothered starting with the secondary attachments yet since I can't figure out how to do the primary attachments yet, but it'd essentially be the same code just with a few differences.

Thanks you!

答案1

得分: 1

这是如何从列表中选择一个随机元素的方法:

  1. List<String> myList = new ArrayList<>();
  2. myList.add("M4A1");
  3. myList.add("Kilo");
  4. myList.add("Grau");
  5. int randomIndex = ThreadLocalRandom.current().nextInt(myList.size());
  6. String randomGun = myList.get(randomIndex);

randomGun 的值将是这三支枪中的一个。

编辑

要随机选择一个列表,你可以创建一个列表的 Map

  1. Map<Integer, List<String>> myMap = new HashMap<>();
  2. myMap.put(1, Arrays.asList("M4A1", "Kilo", "Grau"));
  3. myMap.put(2, Arrays.asList("Merc", "Ranger", "Commando"));
  4. myMap.put(3, Arrays.asList("50", "60", "70"));
  5. int randomIndex = ThreadLocalRandom.current().nextInt(myMap.size());
  6. List<String> randomList = myMap.get(randomIndex);

对于一个简单的项目来说,这是可以的,但如果你计划扩展这个项目,我建议使用类。

英文:

This is how to select a random element in a list:

  1. List&lt;String&gt; myList = new ArrayList&lt;&gt;();
  2. myList.add(&quot;M4A1&quot;);
  3. myList.add(&quot;Kilo&quot;);
  4. myList.add(&quot;Grau&quot;);
  5. int randomIndex = ThreadLocalRandom.current().nextInt(myList.size());
  6. String randomGun = myList.get(randomIndex);

The value of randomGun will be one of the three guns.

Edit:

To choose randomly a list you can create a Map of lists:

  1. Map&lt;Integer, List&lt;String&gt;&gt; myMap = new HashMap&lt;&gt;();
  2. myMap.put(1, Arrays.asList(&quot;M4A1&quot;,&quot;Kilo&quot;,&quot;Grau&quot;));
  3. myMap.put(2, Arrays.asList(&quot;Merc&quot;,&quot;Ranger&quot;,&quot;Commando&quot;));
  4. myMap.put(3, Arrays.asList(&quot;50&quot;,&quot;60&quot;,&quot;70&quot;));
  5. int randomIndex = ThreadLocalRandom.current().nextInt(myMap.size());
  6. List&lt;String&gt; randomList = myMap.get(randomIndex);

For a simple project is OK, but if you plan to extend this project then I recommend to use classes.

答案2

得分: 0

Sure, here's the translation:

这可以通过将您的值存储在一个可以通过索引访问的集合中来实现,就像您在数组中所做的那样。然后,生成一个介于 0 和最高索引之间的随机整数。从该索引处的集合中取出值。即可。

英文:

This could be achieved by storing your values in a collection that can be accessed by index, as you are doing with your array. Then you generate a random integer between 0 and your highest index. Take the value from the collection at that index. Voila.

huangapple
  • 本文由 发表于 2020年8月3日 08:47:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63222384.html
匿名

发表评论

匿名网友

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

确定