You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java代码需求:实现用户输入拼接1-10随机数生成演唱会ID

修改后的ConcertID代码实现需求

原代码输出格式为Y 1886,需要调整为Y18867格式——去除输入内容间的空格,并在末尾添加1到10之间的随机数。以下是修改后的完整代码:

package stringvars;

import java.util.Scanner;

public class ConcertID {
    public static void main(String[] args) {
        try (Scanner userInput = new Scanner(System.in)) {
            String yourName;
            System.out.print("enter the last letter of your second name: ");
            yourName = userInput.next();

            String yourDOB;
            System.out.print("enter your Year Of Birth: ");
            yourDOB = userInput.next();

            // 生成1到10之间的随机整数
            int randomNum = (int)(Math.random() * 10) + 1;
            // 拼接成无空格格式并加上随机数
            String concertID = yourName + yourDOB + randomNum;
                        
            System.out.println("your concert ID is " + concertID);
        }
    }
}

关键改动说明

  • 去除空格:将原代码中yourName + " " + yourDOB的空格去掉,直接用yourName + yourDOB拼接字符串
  • 生成随机数:使用(int)(Math.random() * 10) + 1生成1-10的整数——Math.random()生成0到1(不含1)的浮点数,乘10后得到0到10(不含10)的范围,强转int后是0-9,加1就得到1-10的随机数
  • 拼接最终ID:把随机数直接追加到无空格的拼接结果后,得到目标格式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 12:48:34