Question: I have this code that prints a user defined sized grid of just random 2s and 1s but I can't figure out why I'm getting

I have this code that prints a user defined sized grid of just random 2s and 1s but I can't figure out why I'm getting this error:

random.java:15: error: method random in class Math cannot be applied to given types;

System.out.print((int)(2 * Math.random(2) +1));

^

required: no arguments

found: int

reason: actual and formal argument lists differ in length

Here is my code:

import java.util.Scanner;

public class random {

public static void main(String[] args)

{

//program prints random grid of only 2s and 1s

Scanner x = new Scanner(System.in);

System.out.println("enter height and width: ");

int height = x.nextInt();

int width = x.nextInt();

for (int i = 1; i <= height; i++)

{

for (int j = 1; j <= width; j++)

{

System.out.print((int)(2 * Math.random(2) +1));

}

System.out.println();

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!