大家好,我是新手,现在我在多态部分,发现一些难以理解的东西,这是我第一次看到Math.random(),问题是为什么讲师使用* 5,为什么不* 6或* 4或其他数字?第二个问题是为什么他特别使用+1? 对不起,我的英语不好 这是我正在谈论的源代码:
package CompositionChallenge;
class Movie {
private String name;
public Movie(String name) {
this.name = name;
}
public String plot() {
return "There is no plot";
}
public String getName() {
return name;
} }
class hacksawRidge extends Movie {
public hacksawRidge() {
super("Hacksaw Ridge");
}
@Override
public String plot() {
return "Talk about world war 2 and how a medical person helps people to live their life";
}}
class Love extends Movie {
public Love() {
super("Love");
}
@Override
public String plot() {
return "talk about a person has sex every single day with his wife and girlfriend";
}}
class TripleThreat extends Movie {
public TripleThreat() {
super("TripleThreat");
}
@Override
public String plot() {
return "The best fighters in the world in one movie";
}}
public class Main {
public static void main(String[] args) {
for (int i = 0; i <= 4; i++) {
Movie movie = randomMovie();
System.out.println(
"Number " + i + " and the random movie is " + movie.getName() + " and its plot is " + movie.plot());
}}
public static Movie randomMovie() {
int randomNumber = (int) (Math.random() * 5) + 1;
System.out.println("Random number generated was " + randomNumber);
switch (randomNumber) {
case 1:
return new Love();
case 2:
return new hacksawRidge();
case 3:
return new Movie("No movie now");
case 4:
return new TripleThreat();
default:
return null;
}
}}