2015-05-24 38 views
-2

我正在製作一個非常簡單的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; 
    } 
} 
+0

什麼範圍的值? – Bohemian

+2

你的人類還沒有'擁有'這些狗。要正確實施所需的方法,您可能必須創建人與動物之間的關係(提示:男性可能擁有動物列表)。 – home

+0

您需要在Human類中實例化一系列Dog對象。 – Tdorno

回答

2

隨機int可以使用java.lang.Math.random()或java.util.Random獲得。

繼承人我將如何得到使用的Math.random()0和大小(另一INT)之間的隨機INT:

int randomInd = (int)((size+1)*Math.random()) //needs casting to int as a double is returned 

而現在使用了java.util.Random:

Random r = new Random(); 
    int randomInd = r.nextInt(size+1); 

您應該意識到,根據上述評論,您的設計可以大大改善。想想你會怎麼回答下面的問題:

  1. 可以一個人有多一個寵物嗎?人能有貓還有狗嗎?
  2. 人類應該包含makeDogNoise()和makeCatNoise()方法,還是隻使用makePetNoise()?
  3. 人,狗,貓更好地描述比複數的對象,如人類
0

試試這個:

 

    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; 
    } 

    public void makeDogMakeNoise(Dogs d){ 
     d.makeNoise(Math.random()); 
    } 
    } 

    public class Dogs { 
    private final String mDogName; 
    private String[] strs= {"barks", "woofs", "whimpers"}; 

    public Dogs(String dogName){ 
     mDogName = dogName; 
    } 
    public String getDogName(){ 
     return mDogName; 
    } 

    public void makeNoise(int n){ 
     System.out.println("Ghost "+strs[(int)(n*(strs.length-1))]); 
    } 
    } 

    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"); 
     firstName.makeDogMakeNoise(firstDog); 

     Dogs secondDog = new Dogs("Paperbag"); 
     Humans secondName = new Humans("Michael"); 
     secondName.makeDogMakeNoise(secondDog); 

     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"); 
     } 
    }  

+0

對於在AmazingPets.java中將其打印到控制檯上的最佳實現方法,您會有什麼建議? – theenigma

+0

我編輯了包含AmazingPets.java的答案 –

0

編輯:誤解你的問題的「隨機數」的一部分。

爲了使人類,告訴他/她的狗之一,以「製造噪音」,他/她需要有擺在首位,以狗的參考。

爲了做到這一點,您可以在Humans類中定義makeDogMakeNoise方法,以便它接受一個參數(即,到狗的引用):

public void makeDogMakeNoise(Dogs dog){ 
    // Tell the specified Dog to make one of n random noises 

    // Generate a random integer between 0 and 9 
    int n = (int)(Math.random() * 10); 

    // Now you can tell the dog to make a noise 
    dog.makeNoise(n); 
} 

你需要定義你的狗類中的makeNoise方法:

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,Cat)。

希望有所幫助。