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: please solve my problem

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

    Default please solve my problem

    public class fizzbuzz {
     
     
    	public static void main(String[] args) {
    		int i = 0;
    		while(i < 100)
    		{
    			i++;
    			if(i % 3 != 0)
    			{
    				System.out.println(i);
    			}
    			else
    				if(i % 5 != 0)
    				{
    					System.out.println(i);
    				}
    				if(i % 3 == 0)
    				{
     
    					System.out.println("FIZZ");
    				}
    				if(i % 5 == 0)
    				{
    					System.out.println("BUZZ");
     
    				}
    				if(i % 3 == 0 && i % 5 == 0)
    				{
    					System.out.println("FIZZBUZZ");
    				}
     
     
    		}		
     
    	}
     
    }

    when the numbers prints out , i don't want it to print the i but just the specific system.out.println command.like the number 3 and 5 shouldn't be printed out but just FIZZ or BUZZ or FIZZBUZZ... please help me
    Last edited by joy1604; July 8th, 2013 at 05:02 AM. Reason: more explanation


  2. #2
    Junior Member
    Join Date
    Apr 2011
    Posts
    4
    My Mood
    Inspired
    Thanks
    0
    Thanked 3 Times in 2 Posts

    Default Re: please solve my problem

    Its not clear what is the problem. The output is as follows (in part). Can you identify what you expect the output to be.

    1
    2
    3
    FIZZ
    4
    5
    BUZZ
    6
    FIZZ
    7
    8
    9
    FIZZ
    10
    BUZZ
    11
    12
    FIZZ
    13
    14
    FIZZ
    BUZZ
    FIZZBUZZ
    ... continues to 100.

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

    Default Re: please solve my problem

    thank you so much
    i wanted it to give me just FIZZ instead of 3 and BUZZ instead 5, but its giving me both. like 3 and FIZZ

    --- Update ---

    This is my Output =
    1
    2
    3
    FIZZ
    4
    5
    BUZZ
    6
    FIZZ
    7
    8
    9
    FIZZ
    10
    BUZZ
    11
    12
    FIZZ
    13
    14
    FIZZ
    BUZZ
    FIZZBUZZ
    16
    17
    18
    FIZZ
    19
    20
    BUZZ
    21
    FIZZ
    22
    23
    24
    FIZZ
    25
    BUZZ
    26
    27
    FIZZ
    28
    29
    FIZZ
    BUZZ
    FIZZBUZZ
    31
    32
    33
    FIZZ
    34
    35
    BUZZ
    36
    FIZZ
    37
    38
    39
    FIZZ
    40
    BUZZ
    41
    42
    FIZZ
    43
    44
    FIZZ
    BUZZ
    FIZZBUZZ
    46
    47
    48
    FIZZ
    49
    50
    BUZZ
    51
    FIZZ
    52
    53
    54
    FIZZ
    55
    BUZZ
    56
    57
    FIZZ
    58
    59
    FIZZ
    BUZZ
    FIZZBUZZ
    61
    62
    63
    FIZZ
    64
    65
    BUZZ
    66
    FIZZ
    67
    68
    69
    FIZZ
    70
    BUZZ
    71
    72
    FIZZ
    73
    74
    FIZZ
    BUZZ
    FIZZBUZZ
    76
    77
    78
    FIZZ
    79
    80
    BUZZ
    81
    FIZZ
    82
    83
    84
    FIZZ
    85
    BUZZ
    86
    87
    FIZZ
    88
    89
    FIZZ
    BUZZ
    FIZZBUZZ
    91
    92
    93
    FIZZ
    94
    95
    BUZZ
    96
    FIZZ
    97
    98
    99
    FIZZ
    100
    BUZZ


    But i want it to be =

    1
    2
    FIZZ
    4
    BUZZ
    FIZZ
    7
    8
    FIZZ
    BUZZ
    11
    FIZZ
    13
    14
    FIZZ
    BUZZ
    FIZZBUZZ
    16
    17
    FIZZ
    19
    BUZZ
    FIZZ
    22
    23
    FIZZ
    BUZZ
    26
    FIZZ
    28
    FIZZ
    BUZZ
    FIZZBUZZ
    31
    32
    FIZZ
    34
    BUZZ
    FIZZ
    37
    38
    FIZZ
    BUZZ
    41
    FIZZ
    43
    44
    FIZZ
    BUZZ
    FIZZBUZZ
    46
    47
    FIZZ
    49
    BUZZ
    FIZZ
    52
    53
    54
    FIZZ
    BUZZ
    56
    FIZZ
    58
    FIZZ
    BUZZ
    FIZZBUZZ
    61
    62
    FIZZ
    BUZZ
    FIZZ
    67
    68
    FIZZ
    BUZZ
    71
    72
    FIZZ
    73
    74
    FIZZ
    BUZZ
    FIZZBUZZ
    76
    77
    FIZZ
    79
    BUZZ
    FIZZ
    82
    83
    84
    FIZZ
    BUZZ
    86
    FIZZ
    88
    FIZZ
    BUZZ
    FIZZBUZZ
    91
    92
    FIZZ
    94
    BUZZ
    FIZZ
    97
    98
    FIZZ
    BUZZ


    i mean just to give me FIZZ if it is divisible by 3 and BUZZ if its divisible by 5 and FIZZBUZZ if its divisible by both 3 and 5. but its giving me both 3 and FIZZ , 5 and BUZZ

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: please solve my problem

    Uses else ifs, start with the most complex, i.e i%3 == 0 && i%5 == 0, then test i%3 == 0, then test i%5 == 0, last else sysout i

  5. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool Re: please solve my problem

    Thanks bro i really appreciate it.... it worked

  6. #6
    Junior Member
    Join Date
    Jul 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: please solve my problem

    o

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: please solve my problem

    @pbj please read The problem with spoonfeeding It is encouraged to provide hints, but please do not give copy-paste code solutions. Many questions are homework assignments

Similar Threads

  1. please solve my problem
    By hapini in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2012, 08:00 AM
  2. Please solve my problem
    By Nurhafizah in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 16th, 2012, 10:11 PM
  3. Need help to solve this problem
    By ahanf in forum Object Oriented Programming
    Replies: 1
    Last Post: June 3rd, 2012, 05:00 AM
  4. need help to solve the problem
    By priyadaradi in forum Member Introductions
    Replies: 1
    Last Post: May 24th, 2012, 10:44 AM
  5. help me to solve my problem
    By miszIna in forum Object Oriented Programming
    Replies: 3
    Last Post: February 14th, 2011, 09:40 AM