Question

I want the integer results to print out into four columns. How can I do this...

I want the integer results to print out into four columns. How can I do this from the following code?

import java.util.Random;

public class RanX {
public static void main(String[] args) {
int x;
int y;
  
   //pull from command line argument
x = Integer.parseInt(args[0]);
y = Integer.parseInt(args[1]);

   //for loop to generate random numbers   
for(int r = 0; r < x; r++){
double newr = Math.random()*y;
       int z = (int) newr;
   System.out.println(z);

   }
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Random;
import java.util.Scanner;

public class RanX {
  public static void main(String[] args) {
    int x;
    int y;

    if(args.length == 2) {
      //pull from command line argument
      x = Integer.parseInt(args[0]);
      y = Integer.parseInt(args[1]);
    }
    else{
      System.out.println("You did not provided two command line arguments");
      Scanner scanner = new Scanner(System.in);
      System.out.print("Enter value for x: ");
      x = scanner.nextInt();
      System.out.print("Enter value for x: ");
      y = scanner.nextInt();
    }

    //for loop to generate random numbers
    for(int r = 0; r < x; r++){
      if(r%4 == 0 && r!=0){
        System.out.println();
      }

      double newr = Math.random()*y;
      int z = (int) newr;
      System.out.print(z+" ");

    }
  }
}

Add a comment
Know the answer?
Add Answer to:
I want the integer results to print out into four columns. How can I do this...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT