3

我似乎有我需要的代码,我可以正确地输出。所以我要放 38 个随机数,但需要将它们放入 7 行中请帮忙。

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));
          }
       }
      }
4

2 回答 2

1

这样做有什么问题?

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

这将简单地打印 38 个随机数,在它打印的每 7 个数字上切换到一个新行。

于 2012-09-08T20:19:58.747 回答
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());
于 2012-09-08T20:28:18.113 回答