Hello, I am stuck with the 7th and 8th steps on the Cumulative project 3 .
https://www.codecademy.com/paths/introduction-to-android-with-java/tracks/programming-logic/modules/cumulative-project-3/projects/unquote-game-logic-pt-i
The exact instruction is
" 7.
Calculate a random number between 0 and max
Use the result from task 6 to calculate a random number between 0 and max
(the parameter you pass into generateRandomNumber()
) and save it to a local double
variable."
Cast the result to an integer.
Ultimately, we need a whole number to decide which question to present next because presenting question number 3.2312
or question 0.7
is impossible (we can’t show 70% of a question… right? ).
Instead, we will use a programming technique called casting to convert one data type to another, in this case, from a double
type to an int
type.
An integer cannot retain any decimal values, so given the doubles above, 3.2312
casts down to 3
, and 0.7
casts down to 0
. In both cases, we lose the decimal value.
To cast, we place the resulting type we desire as a prefix to the original data (in parentheses). Check out this example of casting from an int
to a boolean
:"
I have no idea what these steps want me to do. I’d appreciate any hints . My source code so far is right here:
public class MainActivity {
// Add generateRandomNumber() here
public static int generateRandomNumber(int max){
double value = Math.random() * max;
return (int) value;
}
// Add getGameOverMessage() here
public static void main(String[] args) {
}
}