You need to enable JavaScript to run this app. Java基于Math.random()不使用StringBuilder/数组实现字符串打乱的代码修正
优惠活动
大模型
产品
解决方案
定价
更多

Java基于Math.random()不使用StringBuilder/数组实现字符串打乱的代码修正

符合约束的代码实现
public class loopPrr {
    static String shuffle(int a) {
        String s = "BaaBooDaaDoo";
        String result = "";
        for (int i = 0; i < a; i++) {
            int r = (int)(s.length() * Math.random());
            result = result + s.charAt(r);
        }
        return result;
    }
}

约束符合性说明

  • 未显式创建StringBuilder实例,未调用其任何方法,完全规避禁用要求
  • 未使用任何数组结构存储中间结果或字符
  • 未调用Collections.shuffle等可直接完成打乱、拼接功能的封装工具方法
  • 随机字符索引完全通过Math.random()计算生成,满足随机要求

注:Java编译器对字符串+拼接的隐式优化属于编译层底层行为,源代码层面完全没有主动使用StringBuilder相关API,符合约束规则。

内容的提问来源于stack exchange,提问作者casdsgasd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.23 20:36:04