第6章質問38(ランダムな文字を生成する)
- * 6.38(ランダム文字の生成)リスト6-10 RandomCharacterの方法を使用して、100個の大文字と100個の1桁の数字を1行に10個印刷します。
* 6.38(ランダム文字の生成)リスト6.10のRandomCharacterのメソッドを使用して、大文字を100個、次に1桁を100個、1行に10個印刷します。
- 参照コード:
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');
}
}
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