programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# i want a make a program which take input from the keybord and count the numbers

Greenhorn
Posts: 4

What i want to do is that if the number is greater than 50 to count until 100. else if the number is less than fifty to count for example from 36 to zero.

lowercase baba
Bartender
Posts: 12565
49
Here is your code properly formatted:

Marshal
Posts: 56610
172
Welcome to the Ranch

Look closely at the if. You do something if you have less than 50, and you do something if you have more than 50.
What are you doing if you get 50?

Do you mean if you enter 92 it prints this?
92 93 94 95 96 97 98 99 100
You would have to repeat the code to get that sort of output. Do you know how to use a loop to repeat code?

aris fan
Greenhorn
Posts: 4
Campbell Ritchie wrote:Welcome to the Ranch

Look closely at the if. You do something if you have less than 50, and you do something if you have more than 50.
What are you doing if you get 50?

Do you mean if you enter 92 it prints this?
92 93 94 95 96 97 98 99 100
You would have to repeat the code to get that sort of output. Do you know how to use a loop to repeat code?

Hi and thanks for your mail.

I know that i havent defined the case when the input will be equal to 50, but my main concern is how to make the counters work!

As a result yes i mean what you wrote above.

If i input 92 i want to print out 93 94 95 96 97 98 99 100

Campbell Ritchie
Marshal
Posts: 56610
172
As I said, do you know about repetition with loops?
What are you doing when it says 50? Surely not
50 51 49 52 48 53 47…100 0
That is actually quite easy if you can write a loop.

aris fan
Greenhorn
Posts: 4
Campbell Ritchie wrote:As I said, do you know about repetition with loops?
What are you doing when it says 50? Surely not
50 51 49 52 48 53 47…100 0
That is actually quite easy if you can write a loop.

No i dont know a lot about repetition, but i can do a research and learn.
May you give me a part of code?

I will add a >= to 50 in the first if and <=49 in the second and the porblem will be solved. (i think).

Campbell Ritchie
Marshal
Posts: 56610
172
The easiest way to get 50 included is probably to use a plain if-elserather than if-else-if.Beware of >= and <=. It is much easier to understand > or < than >= and <=.

You can read about loops in the flow control part of the Java Tutorials. Look for “while” and “for”.

aris fan
Greenhorn
Posts: 4
Ok but i still dont know how to make the counters work??

Do i need a new variable?

Campbell Ritchie
Marshal
Posts: 56610
172
aris fan wrote: . . . Do i need a new variable?
Only if you want to print at the end
I have counted 42 times.

I think you need new methods, however: countUp and countDown.

Ranch Hand
Posts: 789
This will come close to doing what I think you want. Note I handled the question of 50 itself and note you really only need one test. If you wanted to do something special with certain numbers you could put the test for them above the first " if" already there. Also note if the printing and counting was more involved you might want to write subroutines for it, especially if you would otherwise have two big chunks of code that were basically the same. But in this case you don't lose anything by just duplicating the code and it couldn't be easier to follow. Also note it won't print any numbers if you enter a number greater than 100 or less than 0. You want to always make sure it doesn't do anything goofy no matter what input you give it. So I've restricted the printing to a certain range of input that makes sense. I'm assuming the scanner thing only accepts numbers. If not, you need to handle some wisecracker giving it letters or a function key or an Enter key by itself, etc.

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.