John Sing wrote:So I have to do a variation of the "FizzBuzz" code and my teacher told us to count from 1 to n. But what does n even mean? Is it a variable for any number I want?
Correct. N is any value. I suggest you code it as a method with N as one of its parameter -- and a test program that passes 100 for N. Once it works, test it with many different values of N, from the test program. And finally, ask the user to input N, and call the method.
Get the value of n from the user via an input box.
Generally, you strive to write programs to be as flexible as possible. I could write a program that solves the fizzbuzz challenge from 1 to 100. I could then write another program that solves it for 1 to 200, and another and another...
but that's boring and a waste of time. What's cool (to a programmer) is to solve it for ANY upper limit. In this case, the code is pretty similar. Instead of a loop from 1 to 100, or 1 to 200, I can make my loop from 1 to n. As long as i set that value of n somehow, somewhere, BEFORE I enter that loop, I'm good for (almost) any number I want.
having said that...
Henry has given you AWESOME advice. write a method that takes a number as a parameter, and have your method work for 1 to that value.
You then can call that method, passing in whatever you want. At first, you would most likely want to pass in something like 10 or 20. Keep it small til you work out the bugs. Once you're confident, pass in 100 or 1000...
Once you have that method rock solid, then you can work on getting the user input. The cool thing there is that if you separate the printing piece from the "get the input" piece, you can replace them at will. The printing part doesn't care if you get the data from the command line, from a GUI, from a database, a webservice call, or what. They point is, once you somehow HAVE the value (even if it's hard-coded while you test), you can pass that into your method to print the answer.
fred rosenberger wrote:You then can call that method, passing in whatever you want. At first, you would most likely want to pass in something like 10 or 20. Keep it small til you work out the bugs. Once you're confident, pass in 100 or 1000...
@John: And just to add to the AWESOME advice you've been given by Henry and Fred:
The next logical stage to that for a Java Programmer would be to create a FizzBuzz object that you initialize with your n value; maybe when you create it.
And that's where the fun really starts...
It may be a little early just yet, but the real power in Java is when you can get objects to do the things you want, not just methods.
In my opinion “too early” means in the first five minutes of learning. Once you have six minutes' experience you should be getting objects to do the work. Obviously simple object; you won't go on to complicated objects until at least ten minutes' experience
Winston Gutkowski wrote:. . . It may be a little early just yet . . .