Results 1 to 5 of 5
- 02-10-2013, 10:38 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 2
- Rep Power
- 0
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");
}
}
}
}
- 02-10-2013, 11:16 PM #2
Member
- Join Date
- Feb 2013
- Posts
- 11
- Rep Power
- 0
Re: Replacing outputs with system.out.println
For this I would do something like this instead of writing all those if statements:
This is an easy way of finding multiples... if the remainder(%) equals 0 then it is a multiple of that number.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); } } } }
If you wanted to just show the multiples change the ELSE statement to:
Java Code:else { continue; }Last edited by Modulus; 02-10-2013 at 11:23 PM.
-
Re: Replacing outputs with system.out.println
@Modulus when you test your code, did it work as you expected for the number 30?
- 02-11-2013, 12:56 AM #4
Member
- Join Date
- Feb 2013
- Posts
- 11
- Rep Power
- 0
Re: Replacing outputs with system.out.println
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); } } }
- 02-11-2013, 12:00 PM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
Similar Threads
-
System.out.println ();
By Twixmaster123 in forum New To JavaReplies: 13Last Post: 03-24-2012, 06:08 AM -
is System.out.print same as System.out.println ?
By usuf in forum New To JavaReplies: 8Last Post: 06-21-2011, 02:21 PM -
Println VS system.out.println
By ccie007 in forum New To JavaReplies: 2Last Post: 05-20-2010, 08:52 AM -
difference between system.out.println() & out.println()
By wickedrahul9 in forum Advanced JavaReplies: 5Last Post: 10-18-2008, 11:06 PM -
System.out.println
By Sniper-X in forum Advanced JavaReplies: 10Last Post: 05-05-2008, 03:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks