好吧,所以我會先顯示我的代碼,然後我會解釋我需要幫助。從int到String的隨機元素?
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.
假設969的int值是一個鏟子。是否有可能使969等於上面的int表中的一個字符串 ?因此,如果我登上969,它會說:「你收到了一個普通的物品,你有一個:鏟子!」
我有點新,所以我不確定這是否可能,但我不想爲每個ID加載if語句。
感謝
您可以使用散列(Set,Map),但這不適用於整數,所以使用Integer類來包裝它。 – Joqus
使用一個'隨機' - 沒有理由有3個... –