我是靠谱客的博主 聪慧睫毛,这篇文章主要介绍第六章第三十八题(生成随机字符)(Generate random characters)第六章第三十八题(生成随机字符)(Generate random characters),现在分享给大家,希望可以做个参考。

第六章第三十八题(生成随机字符)(Generate random characters)

  • *6.38(生成随机字符)使用程序清单6-10RandomCharacter中的方法,打印100个大写字母及100个一位数字,每行打印10个。
    *6.38(Generate random characters) Use the methods in RandomCharacter in Listing 6.10 to print 100 uppercase letters and then 100 single digits, printing ten per line.
  • 参考代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package chapter06; public class Code_38 { public static void main(String[] args) { for (int i = 1; i <= 200; i++) { if (i <= 100) System.out.print(RandomCharacter.getRandomUpperCaseLetter()); else System.out.print(RandomCharacter.getRandomDigitCharacter()); if (i % 10 == 0) System.out.print("n"); } } } class RandomCharacter { public static char getRandomCharacter(char ch1, char ch2) { return (char) (ch1 + Math.random() * (ch2 - ch1 + 1)); } public static char getRandomLowerCaseLetter() { return getRandomCharacter('a', 'z'); } public static char getRandomUpperCaseLetter() { return getRandomCharacter('A', 'Z'); } public static char getRandomDigitCharacter() { return getRandomCharacter('0', '9'); } public static char getRandomCharacter() { return getRandomCharacter('u0000', 'uFFFF'); } }
  • 结果显示:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
FJOYKOMEHM HKYEGHIUML MKFQTUOHRP RWVODHQXUI EMPVKMBSIH UQSKXLXQHF RIZGMWIRYO NKQIMITGMP CSFEPWRRZZ EZDFGHEQUX 7805935339 0190371742 0583184795 6189380079 0458389391 8499297748 5786171408 5156655037 3500072102 7427853825 Process finished with exit code 0

最后

以上就是聪慧睫毛最近收集整理的关于第六章第三十八题(生成随机字符)(Generate random characters)第六章第三十八题(生成随机字符)(Generate random characters)的全部内容,更多相关第六章第三十八题(生成随机字符)(Generate内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部