Tony Docherty wrote:Whenever you find yourself writing the same basic chunk of code again, think I need to putting that code in a method which you can then repeatedly call whenever you need it.
In this case putting the code to get the next input into a method will also make it easier for you to work out how to repeatedly execute the code until you get a valid input.
Alena Net wrote:I still can not do it.
Please can you show the direction ?
This is what I have now, and it works quite wrong.
Alena Net wrote:
My code:
P.s. Tony , you're really great explains.
Thank you so much .
That only improves it slightly. There is a far better and simpler way to ensure one reads an int with a Scanner.S Majumder wrote: . . .
Hi Alena,
I'd like to do a little change on your code :
. . .
S Majumder wrote:
The reason is :
If I input other than integer , the execution will go to the else part and here is the trick .The recursive call has been made .Get the data from the call and return that vale .I guess you understand .
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|