有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

打印格式Java

 out.append("Line " + (j+1) + ": ");  
 System.out.print("Line " + (j+1) + ":");
 for (int i = 0; i < lotto.length;i++){
     lotto[i] = r.nextInt(45)+1;                                                                
     out.append(lotto[i] + " ");
     System.out.print(lotto[i] + " ");
 }

现在打印如下

Line 1: 41 7 38 20 38 39 Line 2: 12 35 5 27 4 33 Line 3: 9 3 10 15 35 2


共 (1) 个答案

  1. # 1 楼答案

    因为您在打印Line n之后正在打印新行

    在打印数字的内部for后面应该使用println

    就像

    // print "Line n"
    System.out.print("Line " + (j+1) + ":");
    
    for (int i = 0; i < lotto.length;i++){
        lotto[i] = r.nextInt(45)+1;                                                             
        out.append(lotto[i] + " ");
        // Print in the same line the numbers
        System.out.print(lotto[i] + " ");
    }
    // Print the new line
    System.out.println();