Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Remainder of random generator

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Remainder of random generator

    Hi, I wonder if anyone could help.

    I'm pretty new to programming. In the process of creating a random month and season generator. Seems to be successful so far. However I'd like it to print out the remaining months of the year that were not selected by the random month generator. Right now it always pints out my current IF statement, regardless of whether it's the correct decision or not.

    Been really stuck on this for the past week. I've tried to store months[monthChooser.nextInt(11) as variable so I could say:

    if months[monthChooser.nextInt(11).equals("Jan"));

    But I can't figure out how to store it as a variable so I can print the remaining months of the year depending on what month the generator selects randomly. Not sure that would even work anyway. Can anyone help me with this? Really stuck!
    Here is what I have so far:


    ___________________________________
    package day1.examples;

    import java.util.Random;

    public class MonthSeason {

    public static void main(String[] args) {

    String[] seasons = {"Spring","Summer","Autumn","Winter"};
    String[] months = {"Jan", "Feb","Mar","Apr","May","Jun","Jul","Aug","Sept"," Nov","Dec"};



    Random seasonChooser = new Random();
    Random monthChooser1 = new Random();



    System.out.print("You've been allocated ");
    System.out.print(months[monthChooser1.nextInt(11)]);
    System.out.print(" which is in ");
    System.out.print(seasons[seasonChooser.nextInt(3)]);
    System.out.println(".");

    System.out.println("The other months of the year are: ");

    if(months.equals("Jan"));
    System.out.println("Feb, Mar, Apr, May, June, Jul, Aug, Sept, Nov, Dec");
    }

    }

    ___________________________________

  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    93
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Remainder of random generator

    if(months.equals("Jan"));
    You have a semicolon at the end of that, which terminates the if statement and the "Feb, Mar, " etc. print statement is not inside of it.

Similar Threads

  1. Random Shape Generator
    By sjdganc in forum What's Wrong With My Code?
    Replies: 29
    Last Post: August 31st, 2014, 07:33 PM
  2. Random Number Generator
    By Rugby_Thompson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 5th, 2013, 12:58 AM
  3. Random Number Generator Always gives 0
    By tlckl3m3elmo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 11th, 2012, 03:09 PM
  4. My random generator isn't working. Please help!
    By tripline in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 5th, 2011, 10:03 AM
  5. random number generator
    By java3 in forum Loops & Control Statements
    Replies: 4
    Last Post: February 21st, 2011, 12:00 PM