• Post Reply Bookmark Topic Watch Topic
  • New Topic

Main Method calling or repeating  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there, hope everyone doing well. I was preparing for OCAJ exam and trying some codes so i can grasp methods properly as this is the most important part of all topics. anyway, coming to my problem with this code. I have found this code online ans trying to modify it if I can. I wanted a simple change with the code which is it will ask every time i finish a calculation that "do you want to calculate more ?". (its like user will again choose an option for calculation). then the main method will repeat until user enter the first number as 0(ZERO). I know it will work just fine with loops such as while or may be if else. but how i can tell the main method to repeat itself. (sounds silly but am still learning... he he ). Please help. Thank you in advance, you guys are very kind here.



 
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of things before I address the question: you should always QuoteYourSources (← that's a link). Where did you get this code? It's not all that great. I would rewrite it if I were you.

But to answer your question. You have basically three choices for loops: for/next, while, and do/while. I know a lot of people here don't like the do/while but I think it's perfect for this situation. How would you use a do/while loop to get main() to repeat?
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code looks messy...

The repeat() method will not work. In that method, you are first creating a variable choice that is initialized to 0 and the next thing you do is check if it is not 0. That is ofcourse always false. Also, it makes no sense to do add(), sub(), mult(), div() all after each other. You didn't want the user to do an add, then a sub, etc. When you read or write code, think in logical steps. What's happening in the repeat() method is not logical at all.

Get rid of the repeat() method.

Put a loop in the main() method, around almost everything in the method. You could for example use a do-while loop.
 
Sheriff
Posts: 4931
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Personally I don't think, that such a training with code like this would help you score higher in OCAJ exam. OCAJ exam is not about such a things how logic of the program should flow. It is more about the Java language itself, technical part of it, compiler behaviour - in different words, unusual situations, corners of the language.
Do you have an exam preparation book? Have you looked at it?

About that code you posted:
Probably it more shows how the code shouldn't be written.

I mean (do you notice what's possibly wrong with it?):
And there are many more problems. Usually such a situations suggests one thing - bin it and start over.
Likely with a pencil and piece of paper by writing down the logical steps in simple english what has to be done (by omitting "how" part).
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And never close a Scanner pointing to System.in. Not even if Eclipse suggests it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!