目录

呈现结果

 TestRandom.java


 

呈现结果

java随机四位数 java生成四位数随机数_java

java随机四位数 java生成四位数随机数_System_02

java随机四位数 java生成四位数随机数_System_03

 TestRandom.java

import java.util.Random;
class RandomDemo {
    public void getRandom(){
        System.out.println("4位随机数:");
        System.out.println("⌈‾‾‾‾‾‾‾‾⌉");
        System.out.print("|  ");
        for(int j=0;j<4;j++) {
            Random rom = new Random();
            int a = rom.nextInt(75) + 48;
            int b = 0;

            while ((a > 57 && a < 65) || (a > 90 && a < 97)) {   //防止出现随机数超出的情况
                b = rom.nextInt(75) + 48;
                a = b;
            }

            char character = (char) a;//强制转换

            System.out.print(character);

        }
        System.out.println("  |");
        System.out.println("⌊________⌋");
    }
    public void getRandom(int number){
        System.out.print("\nn位随机数为:\n");
        System.out.print("⌈");
        for(int l=0;l<number;l++) {
            System.out.print("‾‾‾");
        }
        System.out.print("⌉\n|");
        for(int l1=0;l1<number;l1++) {
            System.out.print(" ");
        }
        for(int k=0;k<number;k++) {
            Random rom = new Random();
            int a = rom.nextInt(75) + 48;
            int b = 0;

            while ((a > 57 && a < 65) || (a > 90 && a < 97)) {   //防止出现随机数超出的情况
                b = rom.nextInt(75) + 48;
                a = b;
            }
            char character = (char) a;//强制转换

            System.out.print(character);

        }
        for(int l2=0;l2<number;l2++) {
            System.out.print(" ");
        }
        System.out.print("|\n⌊");
        for(int l3=0;l3<number;l3++) {
            System.out.print("___");
        }
        System.out.println("⌋");
    }
}
public class TestRandom{
    public static void main(String[] args){
        RandomDemo r=new RandomDemo();
        r.getRandom();

        Random rom1 = new Random();
        int c = rom1.nextInt(17)+4;
        r.getRandom(c);      //重载定义
        System.out.print("\n");
    }
}