Java 随机字符串程序

Java 随机字符串程序,java,netbeans,Java,Netbeans,我试图创建一个程序,从a-z运行5-10行随机文本。我试过运行这个程序,但总是得到一个空白的控制台 public static void main(String[] args) { // TODO code application logic here int numberLines = (int) (Math.random() * 5 + 5); for (int b = 0; b <= numberLines; b++) { int leng

我试图创建一个程序,从a-z运行5-10行随机文本。我试过运行这个程序,但总是得到一个空白的控制台

public static void main(String[] args) {
    // TODO code application logic here
     int numberLines = (int) (Math.random() * 5 + 5);
    for (int b = 0; b <= numberLines; b++) {
         int length = (int) Math.random() * 80;
for (int i = 1; i <= length; i++) {
    int randChar = (int) Math.random() * 26;
    if (randChar == 0) {
        System.out.print("a");
    }
    else if (randChar == 1) {
        System.out.print("b");
    }
    else if (randChar == 2) {
        System.out.print("c");
    }
    else if (randChar == 3) {
        System.out.print("d");
    }
    else if (randChar == 4) {
        System.out.print("e");
    }
    else if (randChar == 5) {
        System.out.print("f");
    }
    else if (randChar == 6) {
        System.out.print("g");
    }
    else if (randChar == 7) {
        System.out.print("h");
    }
    else if (randChar == 8) {
        System.out.print("i");
    }
    else if (randChar == 9) {
        System.out.print("j");
    }
    else if (randChar == 10) {
        System.out.print("k");
    }
    else if (randChar == 11) {
        System.out.print("l");
    }
    else if (randChar == 12) {
        System.out.print("m");
    }
    else if (randChar == 13) {
        System.out.print("n");
    }
    else if (randChar == 14) {
        System.out.print("o");
    }
    else if (randChar == 15) {
        System.out.print("p");
    }
    else if (randChar == 16) {
        System.out.print("q");
    }
    else if (randChar == 17) {
        System.out.print("r");
    }
    else if (randChar == 18) {
        System.out.print("s");
    }
    else if (randChar == 19) {
        System.out.print("t");
    }
    else if (randChar == 20) {
        System.out.print("u");
    }
    else if (randChar == 21) {
        System.out.print("v");
    }
    else if (randChar == 22) {
        System.out.print("w");
    }
    else if (randChar == 23) {
        System.out.print("x");
    }
    else if (randChar == 24) {
        System.out.print("y");
    }
    else if (randChar == 25) {
        System.out.print("z");
    }
    System.out.println();
 }    
}
publicstaticvoidmain(字符串[]args){
//此处的TODO代码应用程序逻辑
int numberLines=(int)(Math.random()*5+5);

对于(int b=0;b我认为您的问题来自于行
int length=(int)Math.random()*80;
。 这使得
length
始终等于0,因为
Math.random()
返回一个介于0.0和1.0之间的
double
,它将被转换为0作为
int

你可以试着加上这样的括号

int length = (int) (Math.random() * 80);

我认为您的问题来自于行
int length=(int)Math.random()*80;
。 这使得
length
始终等于0,因为
Math.random()
返回一个介于0.0和1.0之间的
double
,它将被转换为0作为
int

你可以试着加上这样的括号

int length = (int) (Math.random() * 80);

Math#Random

返回带正号的双精度值,大于或等于0.0且小于1.0

因此,基本上,您对
长度的计算始终生成
0
。返回的双精度始终小于
1
。以下对
int
的转换将生成
0
,因此,您的第二个循环将不会执行

randchar
的计算也会发生同样的情况

要更改它,可以使用
Random

Random r = new Random();
...
...
int randChar = r.nextInt(26);

Math#Random

返回带正号的双精度值,大于或等于0.0且小于1.0

因此,基本上,您对
长度的计算始终生成
0
。返回的双精度始终小于
1
。以下对
int
的转换将生成
0
,因此,您的第二个循环将不会执行

randchar
的计算也会发生同样的情况

要更改它,可以使用
Random

Random r = new Random();
...
...
int randChar = r.nextInt(26);

想想ascii表格并不能真正回答我的问题。你想说什么?如果这只是一个练习,好吧。但如果你想要的只是一个特定大小的随机字母字符串,你最好使用想想ascii表格并不能真正回答我的问题。你想说什么?如果这只是一个exercise,很好。但是如果您想要的只是一个特定大小的随机字母字符串,那么最好使用Doh!当然。非常感谢sonic。看到int randChar=(int)Math.random()*26;Doh!当然了。非常感谢sonic。看到int randChar=(int)Math.random()*26的相同问题;