Results 1 to 5 of 5
  1. #1
    RUReady18 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Replacing outputs with system.out.println

    I need to make a code that replaces every multiple of 5 with "Fizz" every multiple of 6 with "Bang" and 30 with "FizzBang". I have to come up with a code that repalces, but the code that I have (below) prints the number as well as the term, can anyone lead me in the right direction as to how to replace them?? Thanks


    public class FizzBang {
    public static void main(String args[]){
    int counter = 0;
    while (counter < 41){
    System.out.println(counter);
    counter++;
    if(counter == 30){
    System.out.println("FizzBang");
    }
    if(counter == 5){
    System.out.println("Fizz");
    }
    if(counter == 10){
    System.out.println("Fizz");
    }
    if(counter == 15){
    System.out.println("Fizz");
    }
    if(counter == 20){
    System.out.println("Fizz");
    }
    if(counter == 25){
    System.out.println("Fizz");
    }
    if(counter == 35){
    System.out.println("Fizz");
    }
    if(counter == 40){
    System.out.println("Fizz");
    }
    if(counter == 6){
    System.out.println("Bang");
    }
    if(counter == 12){
    System.out.println("Bang");
    }
    if(counter == 18){
    System.out.println("Bang");
    }
    if(counter == 24){
    System.out.println("Bang");
    }
    if(counter == 36){
    System.out.println("Bang");
    }

    }
    }
    }

  2. #2
    Modulus is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Re: Replacing outputs with system.out.println

    For this I would do something like this instead of writing all those if statements:


    Java Code:
    public class Counter 
    {
    	public static void main(String []args)
    	{
    	
    	int counter;
    	String multiple;
    	
    	for(counter = 1; counter < 41; counter++)
    		{	
    			if(counter % 5 == 0)
    			{
    				multiple = ("Fizz");
    				System.out.printf("%d %s%n", counter, multiple);
    			}
    			else if(counter % 6 == 0)
    			{
    				multiple = ("Bang");
    				System.out.printf("%d %s%n", counter, multiple);
    			}
    			else if(counter % 30 == 0)
    			{
    				multiple = ("FizzBang");
    				System.out.printf("%d %s%n", counter, multiple);
    			}
    			else
    			{
    				System.out.printf("%d %n", counter);	
    			}
    		}
    	}
    }
    This is an easy way of finding multiples... if the remainder(%) equals 0 then it is a multiple of that number.

    If you wanted to just show the multiples change the ELSE statement to:

    Java Code:
    else
    {
    continue;	
    }
    Last edited by Modulus; 02-11-2013 at 12:23 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Replacing outputs with system.out.println

    @Modulus when you test your code, did it work as you expected for the number 30?

  4. #4
    Modulus is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Re: Replacing outputs with system.out.println

    Quote Originally Posted by Fubarable View Post
    @Modulus when you test your code, did it work as you expected for the number 30?
    Sorry, it does now. There may still be a better way of doing it??


    Java Code:
    public class Counter 
    {
    	public static void main(String []args)
    	{
    	
    	int counter;
    	String multiple;
    	
    	for(counter = 1; counter < 41; counter++)
    		{
    		if (counter % 30 == 0)
    		{
    			multiple = "FizzBang";
    		}
    		else if (counter % 6 == 0)
    		{
    			multiple = "Bang";
    		}
    		else if (counter % 5 ==0)
    		{
    			multiple = "Fizz";
    		}
    		else
    		{
    			continue;
    		}
    		System.out.printf("%d %s%n", counter, multiple);
    		}
    	}
    }

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Replacing outputs with system.out.println

    We don't tend to supply fully coded solutions here.
    A simple explanation of % and an example should have been sufficient.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. System.out.println ();
    By Twixmaster123 in forum New To Java
    Replies: 13
    Last Post: 03-24-2012, 07:08 AM
  2. Replies: 8
    Last Post: 06-21-2011, 03:21 PM
  3. Println VS system.out.println
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 05-20-2010, 09:52 AM
  4. difference between system.out.println() & out.println()
    By wickedrahul9 in forum Advanced Java
    Replies: 5
    Last Post: 10-19-2008, 12:06 AM
  5. System.out.println
    By Sniper-X in forum Advanced Java
    Replies: 10
    Last Post: 05-05-2008, 04:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •