• Post Reply Bookmark Topic Watch Topic
  • New Topic

do while loop help  RSS feed

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I am writing the basic calculator code with loops, and my loop is compiling properly, however, on line 94, where I have told my do while loop run while menuSelection does not equal six, the program will not end once I input 6 for menuSelection, I am not sure as to what the problem could be, as everything appears to be fine, maybe a new set of eyes could help me find the obvious, thanks much.

 
Pete Letkeman
Ranch Foreman
Posts: 906
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of things:
How does the user know that they can enter 6 to exit? The menu does not state that.

This block of code does not allow the user to enter the number 6:

You could put something like this between line 22 and line 24

To exit the program, if you choose to.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand I could put an if method in their to end the program, however, I was curious as to why the program is not ending without the if method, as I have it written to do { } while(menuSelection!=6),
so I shouldn't have to put an if method in the program to terminate when 6 is entered, the loop should already end when the user enters 6.
 
Pete Letkeman
Ranch Foreman
Posts: 906
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have an loop, starting on line 24, which checks to make sure that the input value is either 1, 2, 3, 4, or 5. When the end users inputs the number 6 this loop asks them to re enter a value.
After enough invalid entries the program exits. What if you changed the while loop starting on line 24 to allow for the number 6?
You would also have to put in some logic around line 38 to see if the user input the number 6 and if they did then don't ask for more input.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>I have inserted my if statement, the program terminates upon 6 being entered, 
>> now I have another problem being that the program now automatically terminates if any number greater than 6 is entered.
>> I cannot seem to find the cause of this, as their is no option anywhere that I can see in my code telling the program to terminate if a number greater than 6 is entered
>> only if 6 is entered, or a number greater than 6 is entered 3 times

 
Pete Letkeman
Ranch Foreman
Posts: 906
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried out your revised code and it works.

However if you choose an option of 6 you are still asked for two different numbers and then the program quits.

Now, what happens when someone enters a letter instead of a number at any one of the inputs?
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What should I do with line 104, as while(menuSelection!=6); is never used
 
Pete Letkeman
Ranch Foreman
Posts: 906
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that you have changed the loop to allow for the menu selection to be 6 you can remove

Then line 104 is valid and you don't need to do anything with it
 
Pete Letkeman
Ranch Foreman
Posts: 906
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that you have it working, and I completely understand that you are a beginner there are some improvements that you could make. Some of these could be more challenging then others.
What happens when you divide a number like 1 by 0? You may not get when you expect.

Improvement #1
Break down your code so that it uses methods.
One method for each of the following:
  • Add
  • Subtract
  • Multiply
  • Divide
  • Random Number

  • This is actually called refactoring and it allows you to focus solely on one aspect of the program at a time.
    I know that some IDEs like IntelliJ and Eclipse can help with refactoring allowing this to be done with a few menu commands.

    Improvement #2
    Currently when you type in a letter instead of a number the program stops with an exception.
    Clearly the program asks the end user to enter in a number, but not all users will follow this.

    Improvement #3
    Ask yourself if all of the code comments are needed. If they are not needed then remove them.
     
    Campbell Ritchie
    Marshal
    Posts: 56545
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would suggest you keep the != 6. As PL has suggested, print something with “6 to quit” in.
    Don't use System.exit to quit. Go to the end of your loop and then to the end of your method.
    Only use the \r and \n characters if somebody says they want CR or LF or CRLF. You don't need them after println() anyway. Use System.out.printf and the %n tag instead.
     
    kennith stomps
    Ranch Hand
    Posts: 160
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Great advice.
    >>the way that I have this written with the switch statement, the answer always comes out to be 0.
    >> Reason being is that I set num1 and num2 to 0, lines 58
    >>which is required, or else I receive an error that num1 and num2 cannot be resolved to a variable on lines 59-62
    >> I am not sure how to go about solving this other than changing all my switch cases to if statements, which does not seem like the right idea.
    >>as for functions,  I am required to not use functions yet
     
    Pete Letkeman
    Ranch Foreman
    Posts: 906
    26
    Android Chrome IntelliJ IDE Java MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I would stick with the switch/case structure, this is usually easier to follow when you get into multiple comparisons without testing ranges and decimal numbers.
    I understand that the teacher/instructor is trying to make a point without using methods/functions. However you could complete your homework and then play around with methods/functions to see how they can help.

    Did you have a question in your last post? If so what exactly was it?

    Try this block of code out somewhere to see what happens:

    The results may not be what you expect.
     
    kennith stomps
    Ranch Hand
    Posts: 160
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am not sure how to fix the problem of every answer coming out to equal 0.
     
    Pete Letkeman
    Ranch Foreman
    Posts: 906
    26
    Android Chrome IntelliJ IDE Java MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here are the lines which are giving you problems:

    Right now, no matter what you are doing math that you don't need done.
    you should move the lines to case blocks and only do the math after num1 and num2 have values.
    For instance, move "double addAns = num1+num2;" to line 84 then print the result.
    You will have to move lines 64-66 as well. Once you do this things should work correctly again.
     
    kennith stomps
    Ranch Hand
    Posts: 160
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for your help, pete!
     
    Pete Letkeman
    Ranch Foreman
    Posts: 906
    26
    Android Chrome IntelliJ IDE Java MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are welcome. I'm glad I was able to help you out and now you can move on to other programming problems.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!