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

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

Random Selection from Random List of Strings (JAVA)

问题

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

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

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

主武器M4A1
主武器握把Commando
主武器激光主武器枪托无枪托附件
主武器弹夹50发
主武器后握把Stippled
主武器枪管大枪管

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

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

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

import java.util.concurrent.ThreadLocalRandom;

public class RandomClass {

    public static void main(String[] args) {
        System.out.println("主武器:" + randomPrimaryGun());
        System.out.println("主要附件:" + primaryAttachments());
        System.out.println("副武器:" + randomSecondaryGun());
    }

    public static String randomPrimaryGun() {
        String[] primaryGuns = {"M4A1", "Kilo", "Grau", "Bruen", "FAL", "M13"};
        int randIdx = ThreadLocalRandom.current().nextInt(primaryGuns.length);
        String randPrimary = primaryGuns[randIdx];
        return randPrimary;
    }

    public static String randomSecondaryGun() {
        String[] secondaryGuns = {"MP5", "P90", "AUG", "Uzi", "MP7"};
        int randIdx = ThreadLocalRandom.current().nextInt(secondaryGuns.length);
        String randSecondary = secondaryGuns[randIdx];
        return randSecondary;
    }

    public static String primaryAttachments() {
        String[] primaryGrip = {"Merc", "Ranger", "Commando", "None"};
        String[] primaryLaser = {"Tac", "5mw", "None"};
        String[] primaryStock = {"无枪托附件", "无枪托", "快速枪托"};
        String[] primaryMag = {"50发", "60发"};
        String[] primaryRearGrip = {"Stippled", "None"};
        String[] primaryBarrel = {"大枪管", "短枪管"};

        int randIdx0 = ThreadLocalRandom.current().nextInt(primaryGrip.length);
        int randIdx1 = ThreadLocalRandom.current().nextInt(primaryLaser.length);
        int randIdx2 = ThreadLocalRandom.current().nextInt(primaryStock.length);
        int randIdx3 = ThreadLocalRandom.current().nextInt(primaryMag.length);
        int randIdx4 = ThreadLocalRandom.current().nextInt(primaryRearGrip.length);
        int randIdx5 = ThreadLocalRandom.current().nextInt(primaryBarrel.length);

        String grip = primaryGrip[randIdx0];
        String laser = primaryLaser[randIdx1];
        String stock = primaryStock[randIdx2];
        String mag = primaryMag[randIdx3];
        String rearGrip = primaryRearGrip[randIdx4];
        String barrel = primaryBarrel[randIdx5];

        // 在此处构建你的输出字符串
    }
}

我知道我需要一些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:

Primary Gun: M4A1
Primary Grip: Commando
Primary Laser: None
Primary Stock: No Stock Attachment
Primary Mag: 50
Primary Rear Grip: Stippled
Primary Barrel: Big Barrel 

As of now it just outputs something like

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)

import java.util.concurrent.ThreadLocalRandom;
public class RandomClass {
public static void main(String[] args) {
System.out.println("Primary: " + randomPrimaryGun());
System.out.println("Attachments: " + primaryAttachments());
System.out.println("Secondary: " + randomSecondaryGun());
}
public static String randomPrimaryGun() {
String[] primaryGuns = {"M4A1", "Kilo", "Grau", "Bruen", "FAL", "M13"};
int randIdx = ThreadLocalRandom.current().nextInt(primaryGuns.length);
String randPrimary = primaryGuns[randIdx];
return randPrimary;
}
public static String randomSecondaryGun() {
String[] secondaryGuns = {"MP5", "P90", "AUG", "Uzi", "MP7"};
int randIdx = ThreadLocalRandom.current().nextInt(secondaryGuns.length);
String randSecondary = secondaryGuns[randIdx];
return randSecondary;
}
public static String primaryAttachments() {
String[] primaryGrip = {"Merc ", "Ranger ", "Commando ", "None "};
String[] primaryLaser = {"Tac ", "5mw ", "None "};
String[] primaryStock = {"No Stock Attachment ", "No Stock ", "Fast stock "};
String[] primaryMag = {"50 ", "60 "};
String[] primaryRearGrip = {"Stippled ", "None "};
String[] primaryBarrel = {"Big Barrel", "Short Barrel"};
int randIdx0 = ThreadLocalRandom.current().nextInt(primaryGrip.length);
int randIdx1 = ThreadLocalRandom.current().nextInt(primaryLaser.length);
int randIdx2 = ThreadLocalRandom.current().nextInt(primaryStock.length);
int randIdx3 = ThreadLocalRandom.current().nextInt(primaryMag.length);
int randIdx4 = ThreadLocalRandom.current().nextInt(primaryRearGrip.length);
int randIdx5 = ThreadLocalRandom.current().nextInt(primaryBarrel.length);
String grip = primaryGrip[randIdx0];
String laser = primaryLaser[randIdx1];
String stock = primaryStock[randIdx2];
String mag = primaryMag[randIdx3];
String rearGrip = primaryRearGrip[randIdx4];
String barell = primaryBarrel[randIdx5];
}
}

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

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

List<String> myList = new ArrayList<>();
myList.add("M4A1");
myList.add("Kilo");
myList.add("Grau");

int randomIndex = ThreadLocalRandom.current().nextInt(myList.size());

String randomGun = myList.get(randomIndex);

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

编辑

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

Map<Integer, List<String>> myMap = new HashMap<>();
myMap.put(1, Arrays.asList("M4A1", "Kilo", "Grau"));
myMap.put(2, Arrays.asList("Merc", "Ranger", "Commando"));
myMap.put(3, Arrays.asList("50", "60", "70"));

int randomIndex = ThreadLocalRandom.current().nextInt(myMap.size());

List<String> randomList = myMap.get(randomIndex);

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

英文:

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

List&lt;String&gt; myList = new ArrayList&lt;&gt;();
myList.add(&quot;M4A1&quot;);
myList.add(&quot;Kilo&quot;);
myList.add(&quot;Grau&quot;);
int randomIndex = ThreadLocalRandom.current().nextInt(myList.size());
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:

Map&lt;Integer, List&lt;String&gt;&gt; myMap = new HashMap&lt;&gt;();
myMap.put(1, Arrays.asList(&quot;M4A1&quot;,&quot;Kilo&quot;,&quot;Grau&quot;));
myMap.put(2, Arrays.asList(&quot;Merc&quot;,&quot;Ranger&quot;,&quot;Commando&quot;));
myMap.put(3, Arrays.asList(&quot;50&quot;,&quot;60&quot;,&quot;70&quot;));
int randomIndex = ThreadLocalRandom.current().nextInt(myMap.size());
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:

确定