我创建了一个学生注册项目,我需要为那些在我的网站上注册的人提供注册号.我的注册 ID 应该是 ABC0000001 等等.我尝试了以下代码来生成注册号.
I have created a students registration project and I need to give registration number on my own for those who are registering in my website. My Registration Id should be ABC0000001 so on. I tried this following code to generate reg no.
for(int i=0000001;i<3;i++){ System.out.println("ABC"+i); }但我得到的输出为ABC1ABC2ABC3.这是不需要的.请指教.
But I got output as ABC1 ABC2 ABC3. This is not needed. Please advice.
推荐答案数字没有重要的前导零.(没关系,代码实际上在 Java 中使用了 八进制 数字文字.)
Numbers do not have significant leading zeros. (Never mind that the code actually uses an octal number literal in Java.)
参见 String.format 和 格式语法.特别是,'0' flag、width 修饰符和 'd'(十进制整数)转换在这里很有用.
See String.format and the Format Syntax. In particular, '0' flag, width modifier, and the 'd' (decimal integer) conversion are useful here.
for(int i=1; i<3; i++){ // % - start of format // 0 - 0-pad the result // 7 - set result width to 7 characters wide // d - display as decimal integer String id = String.format("ABC%07d", i); System.out.println(id); }更多推荐
有没有办法在java中生成结合字母的ID?
发布评论