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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started