Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Business Process Driven Database Design With Oracle PL SQL

Authors: Rajeev Kaula

1st Edition

1795532386, 978-1795532389

More Books

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago