#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0

    Help me with code


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


    }

    }

    }

    the code prints out my x when its supposed to just print out the only specific system.out.println line meant for it
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Please edit your post and wrap the code with code tags to make it easier to read and understand.

    its supposed to just print out the only specific system.out.println line meant for it
    What output are you expecting? Post the program's current output and explain what output you want to see instead.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    Please edit your post and wrap the code with code tags to make it easier to read and understand.


    What output are you expecting? Post the program's current output and explain what output you want to see instead.
    thank you so much sir. my output is this

    Code:
    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
    but i want the output to be

    Code:
    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

    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
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    It looks like the code should always be printing the value of i and not restricting when it prints by using an if statement.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    It looks like the code should always be printing the value of i and not restricting when it prints by using an if statement.
    i really don't have any idea of how else to write the program it should just give me "i" when its not divisible by 3 or 5 or both and give me just FIZZ When its divisible by 3 , Just BUZZ when its divisible by 5 and FIZZBUZZ when its divisible by both 3 and 5
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Your desired output shows that the numbers are always being printed. Is that what you meant?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    Your desired output shows that the numbers are always being printed. Is that what you meant?

    i want to write a program that will print out 1 - 100 and if it any of the number is divisible by 3 it should print out FIZZ and if any number is divisble 5 print BUZZ and if any number divisible by both 3 and 5 pint FIZZBUZZ. that what i want
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    program that will print out 1 - 100
    So remove the if statements that control when i is printed so that it always prints.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    5
    Rep Power
    0
    Originally Posted by NormR
    So remove the if statements that control when i is printed so that it always prints.
    K bro ...
    thanks so much man

IMN logo majestic logo threadwatch logo seochat tools logo