随机Int作为参数Java
我正在制作一个非常简单的Java应用程序,名为“神奇宠物”。它涉及人类及其宠物(猫或狗)。在这种情况下,我们与狗打交道。我如何为人类创建一个实例方法(称为makeDogMakeNoise),它调用狗的makeNoise并将一个随机整数作为参数传递?makeNoise方法将随机噪声字符串打印到控制台。例如“鬼吠”、“鬼呜呜、“鬼呜呜”。由于我似乎无法在网上找到任何可靠的资源,请任何人在这件事上提供帮助。提前谢谢 AmazingPets.java随机Int作为参数Java,java,oop,instance-method,Java,Oop,Instance Method,我正在制作一个非常简单的Java应用程序,名为“神奇宠物”。它涉及人类及其宠物(猫或狗)。在这种情况下,我们与狗打交道。我如何为人类创建一个实例方法(称为makeDogMakeNoise),它调用狗的makeNoise并将一个随机整数作为参数传递?makeNoise方法将随机噪声字符串打印到控制台。例如“鬼吠”、“鬼呜呜、“鬼呜呜”。由于我似乎无法在网上找到任何可靠的资源,请任何人在这件事上提供帮助。提前谢谢 AmazingPets.java public class AmazingPets {
public class AmazingPets {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Welcome to Pets and Humans! Created By Marc Beepath.\n____________________________\n");
Dogs firstDog = new Dogs("Ghost");
Humans firstName = new Humans("Alex");
Dogs secondDog = new Dogs("Paperbag");
Humans secondName = new Humans("Michael");
Cats firstCat = new Cats("Tom");
Cats secondCat = new Cats("Mr Furball");
Humans thirdName = new Humans("Bryan");
Humans fourthName = new Humans("Julie");
System.out.printf("%s's dog's name is %s.\n", firstName.getHumanName(), firstDog.getDogName());
System.out.printf("%s's dog's name is %s.\n", secondName.getHumanName(), secondDog.getDogName());
System.out.printf("%s's cat's name is %s.\n", thirdName.getHumanName(), firstCat.getCatName());
System.out.printf("%s's cat's name is %s.\n", fourthName.getHumanName(), secondCat.getCatName());
System.out.printf("\n\nHow many Humans have been created? To get your answer type in the console 'population'. ");
Scanner scan = new Scanner(System.in);
String myLine = scan.nextLine();
String pop = "population";
if (myLine.equalsIgnoreCase(pop)) {
System.out.printf("There are %s Humans.\n", Humans.populationCount());
} else {
System.out.printf("There was an error getting the Population.\n");
}
}
Humans.java
public class Humans {
private String mHumanName;
private static int humanCount = 0;
public Humans(String humanName){
mHumanName = humanName;
humanCount++;
}
public String getHumanName(){
return mHumanName;
}
public static int populationCount() {
return humanCount;
}
}
Dogs.java
public class Dogs {
private final String mDogName;
public Dogs(String dogName){
mDogName = dogName;
}
public String getDogName(){
return mDogName;
}
}
可以使用java.lang.Math.random()或java.util.random获得随机整数 下面是如何使用Math.random()获得介于0和size(另一个int)之间的随机整数: 现在使用java.util.Random:
Random r = new Random();
int randomInd = r.nextInt(size+1);
你应该意识到,基于上面的评论,你的设计可以得到很大的改进。想想你会怎么做
回答以下问题:
public void makeNoise(int n){
/*
* Do what you need to for this Dog to "make a noise"
*/
}
您现在可以执行以下操作:
Humans johnSnow = new Humans("John Snow");
Dogs ghost = new Dogs("Ghost");
// Tell the dog to make a noise
johnSnow.makeDogMakeNoise(ghost);
也可以考虑把你的类名改成复数形式(用人、狗、猫代替)。
希望这能有所帮助。什么范围的价值观?你的人类还没有“拥有”狗。要正确实施所需的方法,你可能需要在人和动物之间建立一种关系(提示:人类可能拥有一系列动物)。您需要在Human类中实例化一系列Dog对象。类名应为Singular。您认为在AmazingPets.java中实现这一点的最合适方法是什么,以便将其打印到控制台上?
public void makeNoise(int n){
/*
* Do what you need to for this Dog to "make a noise"
*/
}
Humans johnSnow = new Humans("John Snow");
Dogs ghost = new Dogs("Ghost");
// Tell the dog to make a noise
johnSnow.makeDogMakeNoise(ghost);