我正在製作一個非常簡單的Java應用程序,名爲「Amazing Pets」。它涉及人類和他們的寵物(貓或狗)。在這種情況下,我們正在處理狗。如何爲人類創建一個實例方法(稱爲makeDogMakeNoise),該方法調用Dog上的makeNoise並傳遞一個隨機整數作爲參數? makeNoise方法將隨機噪聲字符串輸出到控制檯。例如,「鬼樹皮」,「鬼緯線,‘鬼嗚咽’。任何人都可以請協助在這個問題上,我似乎無法在網上找到任何可靠的資源?謝謝你在前進。隨機Int作爲參數Java
AmazingPets的.java
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;
}
}
什麼範圍的值? – Bohemian
你的人類還沒有'擁有'這些狗。要正確實施所需的方法,您可能必須創建人與動物之間的關係(提示:男性可能擁有動物列表)。 – home
您需要在Human類中實例化一系列Dog對象。 – Tdorno