Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
随机Int作为参数Java_Java_Oop_Instance Method - Fatal编程技术网

随机Int作为参数Java

随机Int作为参数Java,java,oop,instance-method,Java,Oop,Instance Method,我正在制作一个非常简单的Java应用程序,名为“神奇宠物”。它涉及人类及其宠物(猫或狗)。在这种情况下,我们与狗打交道。我如何为人类创建一个实例方法(称为makeDogMakeNoise),它调用狗的makeNoise并将一个随机整数作为参数传递?makeNoise方法将随机噪声字符串打印到控制台。例如“鬼吠”、“鬼呜呜、“鬼呜呜”。由于我似乎无法在网上找到任何可靠的资源,请任何人在这件事上提供帮助。提前谢谢 AmazingPets.java public class AmazingPets {

我正在制作一个非常简单的Java应用程序,名为“神奇宠物”。它涉及人类及其宠物(猫或狗)。在这种情况下,我们与狗打交道。我如何为人类创建一个实例方法(称为makeDogMakeNoise),它调用狗的makeNoise并将一个随机整数作为参数传递?makeNoise方法将随机噪声字符串打印到控制台。例如“鬼”、“鬼呜呜、“鬼呜呜”。由于我似乎无法在网上找到任何可靠的资源,请任何人在这件事上提供帮助。提前谢谢

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

可以使用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);
你应该意识到,基于上面的评论,你的设计可以得到很大的改进。想想你会怎么做 回答以下问题:

  • 一个人可以养一只以上的宠物吗?一个人可以养一只猫和一只狗吗
  • Human类应该包含makeDogNoise()和makeCatNoise()方法,还是只包含makePetNoise()方法
  • “人”、“狗”、“猫”比“人”等复数形式更好地描述对象
  • 试试这个:

    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"); } } 公共阶层的人{ 私有字符串名称; 私有静态int humanCount=0; 公共人员(字符串人名){ mHumanName=人名; humanCount++; } 公共字符串getHumanName(){ 返回名称; } 公共静态整型填充计数(){ 返回人名计数; } 公共空间制造噪音(狗d){ d、 makeNoise(Math.random()); } } 公家犬{ 私有最终字符串mDogName; 私有字符串[]strs={“汪汪”、“呜呜”、“呜咽”}; 公狗(串狗名){ mDogName=dogName; } 公共字符串getDogName(){ 返回mDogName; } 公共空间噪音(整数n){ 系统输出println(“重影”+strs[(int)(n*(strs.length-1))); } } 公共级惊艳宠物{ /** *@param指定命令行参数 */ 公共静态void main(字符串[]args){ System.out.println(“欢迎来到宠物和人类!由Marc Beepath创建。\n\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu\n”); 第一条狗=新狗(“鬼”); Humans firstName=新人类(“Alex”); firstName.makeDogMakeNoise(firstDog); 第二只狗=新狗(“纸袋”); 人类secondName=新人类(“迈克尔”); secondName.makeDogMakeNoise(secondDog); 猫第一猫=新猫(“汤姆”); 第二只猫=新猫(“福尔鲍尔先生”); 人类第三名=新人类(“布莱恩”); 人类第四名=新人类(“朱莉”); System.out.printf(“%s的狗的名字是%s。\n”,firstName.getHumanName(),firstDog.getDogName()); System.out.printf(“%s的狗的名字是%s。\n”,secondName.getHumanName(),secondDog.getDogName()); System.out.printf(“%s的猫的名字是%s。\n”,thirdName.getHumanName(),firstCat.getCatName()); System.out.printf(“%s的猫的名字是%s。\n”,fourthName.getHumanName(),secondCat.getCatName()); System.out.printf(“\n\n创建了多少人?在控制台“人口”中获取答案类型”); 扫描仪扫描=新扫描仪(System.in); 字符串myLine=scan.nextLine(); 字符串pop=“人口”; if(myLine.equalsIgnoreCase(pop)){ System.out.printf(“有%s个人。\n”,Humans.populationCount()); }否则{ System.out.printf(“获取填充时出错。\n”); } } 编辑:误读问题的“随机数”部分

    为了让一个人告诉他/她的狗“制造噪音”,他/她首先需要有一个关于狗的参考

    为此,您可以在Humans类中定义makeDogMakeNoise方法,以便它接受一个参数(即对狗的引用):

    您需要在Dogs类中定义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对象。类名应为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);