从int到String的随机元素?

 哇哈哈啦啦啦啦_729 发布于 2023-02-08 15:59

好的,所以我先显示我的代码,然后我会解释我需要帮助的内容.

  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值是一个Spade.我可以从上面的一个int表中使969等于一个字符串数组吗?

所以,如果我登陆969,它会说:"你收到了正常的物品.你有一个:锹!"

我有点新,所以我不确定这是否可行,但我不想为每个ID加载if语句.

谢谢

1 个回答
  • 看来你只需要字符串数组而不是整数数组:

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

    阅读有关数组的教程.

    2023-02-08 16:01 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有