This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

While and Do-While Question

 
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!

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


---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
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.
 
lowercase baba
Posts: 12753
51
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:

 
Marshal
Posts: 65050
247
  • 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
Marshal
Posts: 65050
247
  • 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.
 
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
Marshal
Posts: 65050
247
  • 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
Boost this thread!