简体   繁体   中英

How to write assign a random number to an object

I am having trouble trying with all of question 5(picture of questions attached). I'm not sure what to write for question 5. Any help would be great! This is my first time posting!

MyQuestion


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("The Program is Starting..."); 

        PolarBear polarBear = new PolarBear();      

        String[] week = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
            "Friday", "Saturday"};

        for (String daysOfWeek : week) {
            System.out.println (daysOfWeek) ;

        }
        Random randomGenerator = new Random();
        this.



        System.out.println("PolarBear Calories:" + polarBear.getCalorieBank());
        System.out.println("The"
                + " Program Will Loop Through the Days of the Week");       
        System.out.println("Program complete.");          
    }
}

Are you just asking how to generate a random number? You can start with the question here .

As for the specific Seal and Polar Bear parts, it's impossible to give you pointers without knowing what those classes look like, but you should be using the instance variables and methods of those classes to perform the actions you're supposed to.

so as it is a learning question, i am trying to post only a small part of what you might need

for (String daysOfWeek : week) {
        System.out.println (daysOfWeek) ;
        Random randomGenerator = new Random();
        int num = randomGenerator.nextInt((4 - 1) + 1) + 1;
        if(num == 4) {
            System.out.println ("Polar Bear being fed on "+ daysOfWeek) ;
            System.out.println ("Polar Bear calorie is "+ polarBear.addCalorie()) ;// add the calorie value
        }
        else
        {
            System.out.println ("Polar Bear not fed on "+ daysOfWeek) ;
            System.out.println ("Polar Bear calorie is "+ polarBear.getCalorieBank()) // show the calorie value;
        }

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM