gpt4 book ai didi

java - 从 int 到 String 的随机元素?

转载 作者:行者123 更新时间:2023-12-02 13:24:25 24 4
gpt4 key购买 nike

好的,我将首先展示我的代码,然后我会解释我需要什么帮助。

  Random isServerChance = new Random();
Random isGood = new Random();
Random isBad = new Random();

// Some IDs which are considered "Good"
int isGoodTable[] =
{ 1038, 1040, 1042, 1044 };
// Some IDs which are considered "Bad"
int isBadTable[] =
{ 6570, 6585, 952, 969 };
// Based on this roll, will pick which cat is used. Good, or Bad.
int isServerRoll = isServerChance.nextInt(6);
// If it's > 3 it's going to be a "Good" item, if it's not, it's a bad
// item.
if (isServerRoll > 3)
{

int isGoodValue = isGood.nextInt(isGoodTable.length);
System.out.println("You've received a rare item! You got a: " + isGoodTable[isGoodValue]);

} else
{
int isBadValue = isBad.nextInt(isBadTable.length);
System.out.println("You've received a normal item. You've got a: " + isBadTable[isBadValue]);

}

}

好的,这将打印:您收到了一个正常的元素。你有:969 ||您收到了一件稀有元素!您得到的是:1042。

假设 int 值 969 是一个 Spade。我可以让 969 等于一个字符串数组吗来自上面的 int 表之一?

所以,如果我登陆 969,它会说:“您收到了一件普通元素。您有一个:黑桃!”

我是个新人,所以我不确定这是否可能,但我不想为每个 ID 编写大量 if 语句。

谢谢

最佳答案

看来您只是想要字符串数组而不是整数数组:

String[] isBadTable = { "Foo", "Bar", "Baz", "Spade" };

阅读tutorial on arrays .

关于java - 从 int 到 String 的随机元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20734750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com