• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Fizzbuzz type problem

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to write a program that takes input and print Fizz for every power of 3 within 10 segment like the following:
1 2 Fizz 4 5 6 7 8 Fizz 10 11 Fizz 13 14 15 16 17 Fizz 19 20...
however I can not make the logic work at line 18

 
Marshal
Posts: 72619
317
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please explain what you are doing. You aren't printing “Fizz” for powers of 3, because 18 isn't a power of 3, and you aren't printing multiples of 3, because you are printing 15 as a number. I can't see how line 18 relates to a FizzBuzz program. Please don't simply write code, but what you want to do. Only when you have that clear can you work out how to do it.
 
onygen nari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the power of 3 is 9 right!? I want to print fizz based on that now when I reach 10 I want the power counter start once again so 12, 18 are just like 3, 9,
what I want to achieve is to print  like the following but with input not simple loop
1 2 Fizz 4 5 6 7 8 Fizz 10 11 Fizz 13 14 15 16 17 Fizz 19 20

Apologize if I have trouble in explaining it well and thank you for trying to help me
 
Saloon Keeper
Posts: 8079
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

onygen nari wrote:so 12, 18 are just like 3, 9

What do you mean "just like"?

Why do you ask for user input inside an infinite loop? What is "counter" used for?
 
onygen nari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
have changed my question to be as simple as possible.
it is possible to achieve the result I want? because I have been trying for days without success
 
Carey Brown
Saloon Keeper
Posts: 8079
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

onygen nari wrote:I want to write a program that takes input and print Fizz like the following:
1 2 Fizz 4 5 6 7 8 Fizz 10 11 Fizz 13 14 15 16 17 Fizz 19 20...

What is the relationship between the input and the printing?
 
onygen nari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

onygen nari wrote:I want to write a program that takes input and print Fizz like the following:
1 2 Fizz 4 5 6 7 8 Fizz 10 11 Fizz 13 14 15 16 17 Fizz 19 20...

What is the relationship between the input and the printing?



I will insert 1,2, and the app will print Fizz, then I will insert 4,5,6,7,8, and the app will print Fizz again....
 
onygen nari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

onygen nari wrote:I want to write a program that takes input and print Fizz like the following:
1 2 Fizz 4 5 6 7 8 Fizz 10 11 Fizz 13 14 15 16 17 Fizz 19 20...

What is the relationship between the input and the printing?



I will delete my post and repost with as simple as possible
 
Carey Brown
Saloon Keeper
Posts: 8079
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

onygen nari wrote:

Carey Brown wrote:

onygen nari wrote:I want to write a program that takes input and print Fizz like the following:
1 2 Fizz 4 5 6 7 8 Fizz 10 11 Fizz 13 14 15 16 17 Fizz 19 20...

What is the relationship between the input and the printing?



I will insert 1,2, and the app will print Fizz, then I will insert 4,5,6,7,8, and the app will print Fizz again....

Why not have a loop counter to do the "input" then?
 
onygen nari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:

onygen nari wrote:

Carey Brown wrote:

onygen nari wrote:I want to write a program that takes input and print Fizz like the following:
1 2 Fizz 4 5 6 7 8 Fizz 10 11 Fizz 13 14 15 16 17 Fizz 19 20...

What is the relationship between the input and the printing?



I will insert 1,2, and the app will print Fizz, then I will insert 4,5,6,7,8, and the app will print Fizz again....

Why not have a loop counter to do the "input" then?



I have changed my question and now what I want to achieve is straightforward, I just want to print Fizz like the following
1 2 Fizz 4 5 6 7 8 Fizz 10 11 Fizz 13 14 15 16 17 Fizz 19 20....
 
Carey Brown
Saloon Keeper
Posts: 8079
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the rule for replacing a number with Fizz ?
 
onygen nari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:What is the rule for replacing a number with Fizz ?



Every third and ninth element within 10 numbers
1 2 , ("Fizz" 3th element), 4,5,6,7,8, ("Fizz" 9th element), (once I reach 10 the rule start once again) 10 , 11, ("Fizz" 3th element),13,14,15,16,17,("Fizz" 9th element),19, 20
etc...
thank you so much for trying to help me
 
Carey Brown
Saloon Keeper
Posts: 8079
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now you're making progress.

I've got to leave for a few hours. Maybe someone else could chime in now. I'll be back later.
 
lowercase baba
Posts: 12981
66
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
always break down your problem into simpler pieces.  code one piece at a time, get it working, then try and add in a little more.  One way to approach this is to look at what you are outputting, and what the pattern is.

For example, given a number (say, x), you want to print x, x+1, "FIZZ", x+ 3, ..., x+7, "Fizz", x+9.  

So, i'd write a method that takes an int parameter and prints the 10 values.

Then, i'd need to call it over and over, but not for input values 1,2,3, but for input values 1, 11, 21...up to whatever value i need.

 
Saloon Keeper
Posts: 6922
163
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
onygen, please do not edit old posts. If you remove the code, then most of the answers no longer make sense.
 
onygen nari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I apologize for the whole chaos happened here as I usually dont use forums,
Can you delete my post please, so I can make new one with non ambiguous question
 
Saloon Keeper
Posts: 4380
163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For me the question is clear. First of all: the numbers 9, 18, 27, 36, ... should be replaced by FIZZ. Now, what criterium do we have here? And then look at the series 0, 3, 9, 12, 18, 21, 27, 30, .... How do we get 3, 12, 21, 30 et cetera?
 
Marshal
Posts: 26525
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moral of the story:

First figure out what you want to do, then write the code to do that.

And if you can't explain to another person what you want to do, then you won't be able to explain it to the Java compiler either.
 
onygen nari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have finally made it, I would thank you all for helping me.
if there are any tips for improving the code, it is appreciated

 
Carey Brown
Saloon Keeper
Posts: 8079
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
onygen nari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Carey Brown the code is starting from 1 and it is based on giving input in order, if you add bigger or smaller number than the number shown in the Terminal then you will get msg: Add in order + the suggested number
 
Paul Clapham
Marshal
Posts: 26525
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

onygen nari wrote:I want to write a program that takes input and print Fizz for every power of 3 within 10 segment like the following:
1 2 Fizz 4 5 6 7 8 Fizz 10 11 Fizz 13 14 15 16 17 Fizz 19 20...



Could I point out that the code you wrote doesn't do that? There's no fizzing going on at all.

And the user has to type in all of the numbers which aren't Fizz in that sequence or be harassed by error messages? That seems to me a very peculiar implementation of the "takes input" requirement.
 
onygen nari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Paul Clapham  I have just changed the word Fizz with Third, Ninth-Elemnt. As for user input requirement, I already meant it by "takes input".
the function of this app is exactly what I was asking for, If you have better way to ask my question to achieve same result or have better implementation then it is appreciated
 
Paul Clapham
Marshal
Posts: 26525
81
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And then you had something about "power of three" in your requirements, which has totally vanished in your final code. Your first few "fizz" numbers are at least divisible by three, although they aren't all powers of three, but the next two are 22 and 28, which are totally unrelated to three in any way.

I'm glad your code is exactly what you wanted. That's the most important thing about code, that it should be suitable for what the specifier asked for. But as for a better way to ask your question: Nobody can read your mind, and it's nobody's job to anyway. It's your job to make your requirements as clear as possible, which you certainly didn't do. For example "takes input" is totally vague, it should continue on to explain what the input is that it takes. But fortunately you were the one doing the implementation so you knew what you meant.

As for a "better implementation", it might be possible to gripe about details. For example why is there a List when its only purpose is to count the number of entries which have been either entered or fizzed? You could simply have used an integer counter instead. But since the requirements are obscure, it's possible that the List has some obscure purpose too. So the implementation is perfectly fine, as I said.
 
fred rosenberger
lowercase baba
Posts: 12981
66
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:And then you had something about "power of three" in your requirements, which has totally vanished in your final code.


i believe what is meant was that in the range 1-10, the powers of 3 are 3 and 9.  so, in each set of 10 numbers, the 3rd and 9th element are changed to "Fizz".
 
onygen nari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@fred rosenberger That is exactly what I wanted/meant.
 
Campbell Ritchie
Marshal
Posts: 72619
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean to start your decades from 1 as one does in normal counting, in which case you will go 121 122 123 124 125 126 127 128 129 130, changing the 123 and 129 to Fizz, or would you start from 0 as computers often do, and change 122 and 128 to Fizz? I think you mean to start from 1.
 
Carey Brown
Saloon Keeper
Posts: 8079
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This outputs what I believe to match your stated requirements.
Output:
My problem with this is that the first row is treated as a special case. It starts at 1, not zero. And it only has 9 members.
 
onygen nari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell Ritchie for the first set I meant to count from 1. but other sets from 0, you can see @Carey Brown solution or mine to see the result.
 
Carey Brown
Saloon Keeper
Posts: 8079
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

onygen nari wrote:Campbell Ritchie for the first set I meant to count from 1. but other sets from 0, you can see @Carey Brown solution or mine to see the result.

I hesitate to call it a "solution" because in all the decades I've seen various incarnations of the FuzzBuzz exercise,  not one involved a special case. I can't help but feel that the requirements are incorrect. But, hey, I've been wrong before.
 
onygen nari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Carey Brown a month ago I had entry test for ,Free advanced level Java course, and it had Entry exam question (I couldn't pass it to be honest ):

""In this exercise, you will implement a popular kids counting game in Java.
The rules of the game are as follows:

In turns, the players start counting upwards beginning with "1".
For every multiple of 3, instead of calling the number itself, the player has to call out "ka"
For every multiple of 5, instead of calling the number itself, the player has to call out "ching!"
Which leads to every multiple of 15 being called out as "ka-ching!"

The first 18 steps of the game thus sound as follows: "1", "2", "ka", "4", ching!", "6", "7", "8", "ka", "ching!", "11", "ka", "13", "14", "ka-ching!", "16", "17", "ka"...

Implement this game in Java such that, when run, it executes the rules of the game as outlined above.""
 
Campbell Ritchie
Marshal
Posts: 72619
317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't answered my question about decades; you will have one decade with only nine numbers in. You need to be precise when you think about your programs, and such imprecise thinking will lead to errors, I am afraid.
Is that the same as you are trying to implement now? That last example is what FizzBuzz usually looks like.

[edit] I see Carey has already said the first line only has nine members.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic