using Math.random() to generate only even and odd numbers in a certain interval
The problem says to
Write a method that takes a
Rectangle
as a parameter, sets i开发者_如何学JAVAts length to a random, even integer between 10 and 20 inclusive, and sets its width to a random, odd integer greater than or equal to 7 and less than 14. UseMath.random()
to generate random numbers.
This method must be called
randomize()
and it must take aRectangle
parameter.
You can call your method in the program's
main
method so you can test whether it works, but you must remove or comment out themain
method before checking your code for a score. You will also need to make sure you do not remove the import statementimport testing.Math
as this is required to check your code for a score.
To reference the documentation for the Rectangle
class, click here.
So far I have
import java.util.Scanner;
import shapes.*;
import testing.Math;
public class U5_L3_Activity_Two{
public static void randomize(Rectangle p) {
int len = (int)(Math.random() * 10);
if (len < 5) {
len += 5;
}
double length = (len * 2);
int wid = (int)(Math.random() * 10);
if (wid < 4) {
wid += 9;
}
if (wid == 7) {
wid = 6;
}
double width = (wid * 2) + 1;
p.setWidth(width);
p.setLength(length);
}
}
I can't pass the test cases.
This test case checks that your randomize method randomly changes the length of the
Rectangle
parameter to an even number.
and
This test case checks that your randomize method randomly changes the width of the
Rectangle
parameter to an odd number.
So I'm pretty sure it just means I'm not correctly making my program even and odd.
精彩评论