Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

While and Do-While Question

 
Chris Hathaway
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again Java Ranch!

Im currently working on a project that lets you add, subtract, multipy, or divide 2 integers, and then gives you the answer

I wanted to put a while or do-while switch on it so that when you get your answer,
it restarts itself and prompts you to pick which operand you want and which two integers to be used, but I havent been
able to figure it out.

Help and suggestions would be great! Please explain your logic behind your reasoning!

Here is my code so far!

--------------------------------------------------------------------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you try and what specific problem are you having? I don't see a while or do/while in that code. Have you gone through a tutorial or textbook section on while and do/while loops? If not, that's your starting point. Then take your best shot, and then post a more specific question if you get stuck.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The real difference between a while and a do-while is the latter is guaranteed to run at least once. a while loop can be written so that it always runs at least once, by doing something like this:

 
Campbell Ritchie
Sheriff
Pie
Posts: 49741
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some poor code style there.
You have far too long a main method. The ideal length for a main method is one statement. You have a lot of repeated code, which ought to be refactored into separate methods. Some of it should be refactored into a utility class (that example has different methods from what you want).
 
Campbell Ritchie
Sheriff
Pie
Posts: 49741
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to use a switch-case block with exit as an option, you can put it inside a while(true) loop, but I doubt the design of a loop which exits from inside itself.
 
dennis deems
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To augment on Campbell's response: repetitious code is a typical beginner's mistake. We all did it when we were starting out, so it's no badge of shame -- just something to start learning to recognize and correct. See that the code in each case of the switch block is almost identical. The only difference is the operation that is performed to determine what value is assigned to answer (and the corresponding symbol that is printed out). Try to spot repetitions like this in your code, and see if there is a way that all the common code could be consolidated. If you change your perspective on this problem somewhat, you may find that you don't need the switch block at all.
 
Chris Hathaway
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your help! Ended up figuring it out myself!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49741
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done How did you manage it?
 
Chris Hathaway
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I got to theory crafting and started trying random stuff, I put the whole code, give or take a few brackets, in a do - while loop, and made sure the "Int choice = 0;" was outside the loop, final code looks like this...

I remember you saying
1. that you doubted the design of a do - while switch that exits from inside itself
2. that my main method was way too long, and alot of repeated code.

Do you think you could elaborate for future reference? Would be very helpful

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic