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 7 of 7

Thread: Conditional Statement Issue *Newbie Help*

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Conditional Statement Issue *Newbie Help*

    I'm trying a basic challenge that involves a program counting to 100, and displaying that output while it counts up.
    • For every multiple of 3; it must print Fizz
    • For every multiple of 5, it must print Buzz
    • For every multiple of 3 & 5 it must print FizzBuzz


    The code I've written handles System.out on the non-multiple numbers, and the FizzBuzz multiples without issue. However, for the multiples of just 3 and 5 it acts as if I didn't write any "else if" condition. I have been unable to figure out what might be causing this! Here is the code.

    class prog{
    	public static void main(String args[]){
     
    	int num = 0;
     
    	while (age < 100){
    	num++;
     
    	if (num % 3 != 0 || num % 5 != 0){
    		System.out.println(num);
    	}
    	else if (num % 3 == 0 && num % 5 != 0){
    		System.out.println("Fizz");
    	}
    	else if (num % 5 == 0 && num % 3 != 0){
    		System.out.println("Buzz");
    	}
    	else if (num % 5 == 0 && num % 3 == 0){
    		System.out.println("FizzBuzz");
    	}
     
    	}
     
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Conditional Statement Issue *Newbie Help*

    Which line(s) are printing in error or not printing as expected?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Conditional Statement Issue *Newbie Help*

    These two blocks

    else if (num % 3 == 0 && num % 5 != 0){
    System.out.println("Fizz");
    }
    else if (num % 5 == 0 && num % 3 != 0){
    System.out.println("Buzz");

    It's as if I never even wrote the code in.

    Ex.
    1,2,3,4,5 .... 13,14,FizzBuzz

    When I'm trying to get 1,2,Fizz,4,Buzz....

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Conditional Statement Issue *Newbie Help*

    Your issue is your first statement. Check your logic:
    1. if num is not a multiple of 3 OR if num is not a multiple of 5
    2. if num is a multiple of 3 AND if num is not a multiple of 5

    Right there is the start of your problem. Your second condition requires num to NOT be a multiple of 5. However, if num is not a multiple of 5, the first if statement's condition is met, so the else statements are never even checked.
    This same logistical problem is in your other conditions.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. The Following User Says Thank You to aussiemcgr For This Useful Post:

    dotChristo (March 7th, 2013)

  6. #5
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: Conditional Statement Issue *Newbie Help*

    Here's the error:
    if (num % 3 != 0 || num % 5 != 0) {
                    System.out.println(num);
                }
    It must be
    if (num % 3 != 0 && num % 5 != 0) {
                    System.out.println(num);
                }

    You see, this condition goes before your "Fizz" and "Buzz" conditions. But "Fizz" is executed only if num % 5 != 0. And if this condition is true, than the erroneous block's condition holds (that is, num % 3 != 0 || num % 5 != 0). The same thing is for the "Buzz" block.

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Conditional Statement Issue *Newbie Help*

    With chains of if/else if statements, the first one that is true will be executed. NONE of the others will be tested.
    If the first if statement is true none of the other if statements will be tested.

    Does this post mean that the first if is true for numbers 1 to 14:
    1,2,3,4,5 .... 13,14,FizzBuzz
    Look at the conditions in the first if and see when it will be false. Right now it is true for numbers 1 thru 14
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Conditional Statement Issue *Newbie Help*

    That makes perfect sense. I can't believe I didn't pick up on that at all. Thanks!

Similar Threads

  1. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  2. Trying to fix a YQL statement - Newbie needs help!
    By ukracer in forum Other Programming Languages
    Replies: 2
    Last Post: April 13th, 2012, 07:35 AM
  3. How to create this conditional expression in Java...?
    By jwall2 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 12th, 2011, 07:41 AM
  4. Conditional statements on Array list objects
    By jaguarpaw in forum Collections and Generics
    Replies: 3
    Last Post: May 10th, 2011, 09:52 AM
  5. [SOLVED] if statement issue
    By Elementality in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 17th, 2011, 03:29 PM