需要帮助我的输出成行和列

时间:2012-09-08 20:13:24

标签: java

我似乎有我需要的代码,我可以正确地进行输出。所以我要输出38个随机数,但是需要将它们排成七行我相信我解决了正确的编码使用的答案然而它没有正确输出或我会在这里得到错误是我的代码想要一些请帮助。

import java.util.Scanner;

public class OneCounter2{

        public static void main(String args[]){ 
            Scanner input = new Scanner( System.in );
            System.out.println(" Product 2: Tires");
            System.out.println("  Random numbers between 1 and 3800 are,");
            for ( int row = 0 ; row < 5 ; row++ )
            {
                // PRINT a row
                for ( int col = 0 ; col < 7 ; col++ )
            }
            for(int i=0; i < 38 ; i++)
            {
                System.out.print( "*" ) ;
             }
              // PRINT newline
               System.out.println( "" ) ;
               System.out.println("  Random Numbers ["+ (i+1) + "] : " + (int)(Math.random()*3800));
          }
       }
      }

2 个答案:

答案 0 :(得分:1)

这样做是怎么回事?

for (int i = 1 ; i < 39 ; i++) {
    System.out.print((int)(Math.random() * 3800) + " ");
    if (i % 7 == 0) System.out.println();
}

这将只打印38个随机数,在每打印7个数字时切换到新行。

答案 1 :(得分:0)

我更喜欢使用stringbuilder并一次显示输出...

StringBuilder sb = new StringBuilder();
   for(int i =0;i<=38;i++)
   {
       sb.append(Math.random() + " ");
       if ( i%7 == 0) sb.append("\n");
   }
   System.out.println(sb.toString());