user1657240
user1657240

Reputation: 31

Need help on making my output into rows and columns

I seem to have the code I need and I can get an out put correctly. So I'm out putting 38 random numbers but need to out put them into rows of seven I believe I solved the answer to the correct coding to use however It's not putting out correctly or I'll get errors here is my code would like some assistance please.

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

Upvotes: 3

Views: 453

Answers (2)

user1631651
user1631651

Reputation: 231

i prefer to use stringbuilder and display output at one go...

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());

Upvotes: 0

arshajii
arshajii

Reputation: 129497

Whats wrong with doing it like this?

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

This will simply print the 38 random numbers, switching to a new line on every 7th number it prints.

Upvotes: 1

Related Questions