• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Subtraction Problem with For-Loop help  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The lab currently generates a program to ask a student to subtract two numbers and told them if they were right or wrong.
I am trying to rewrite this program to first ask for how many correct the student needs to give, then continues to ask the student subtraction until they answer that many correct questions.
How would I implement a for-loop to do so?

 
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you want to use a for-loop?  Do you know the number of times you will iterate through the loop, or is that dependent on something that happens inside the loop?  If the latter, it would be more appropriate to use a while-loop.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and Welcome to the Ranch!
 
Zachary Kang
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If possible, I would like to use a for-loop using the number input by the user which is the number of correct answers needed.  So something using int numberCorrect =0;
 
Zachary Kang
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And thank you! Please let me know if I am being unclear, because this is my first time posting anything in a forum setting regarding JAVA.  I am very amateur at best, but that's why i'm here.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zachary Kang wrote:If possible, I would like to use a for-loop using the number input by the user which is the number of correct answers needed.  So something using int numberCorrect =0;

So you want to keep asking the user questions until he/she gets N questions correct, right?  So, if the user needs to get 10 questions correct and he hasn't answer 10 correctly even after 30 questions, say he has only been able to answer 4 correct, what happens, do you just keep asking questions until he finally gets 10 correct? You could end up asking 50 questions, right? Or 75, or 103, or ... do you get where I'm coming from?

You could do this with a for-loop but it will an unconventional use of the for-loop and it would actually mimic what a while-loop would do. So, why not just go the conventional route and use a while-loop?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a tip for you to simplify your code.  It seems you want to avoid asking a question where the answer is a negative number. That means you want to always subtract the smaller of the two numbers from the bigger of the two.  So instead of having the extra check and basically duplicating the code you have on lines 22 to 32 in the big else block below it on lines 38 to 50, you can just assume one is bigger than the other, check the assumption, and if it's not correct, swap the two numbers. Then you only need lines 22 to 32.

In pseudocode:
 
Zachary Kang
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you want to keep asking the user questions until he/she gets N questions correct, right?  So, if the user needs to get 10 questions correct and he hasn't answer 10 correctly even after 30 questions, say he has only been able to answer 4 correct, what happens, do you just keep asking questions until he finally gets 10 correct? You could end up asking 50 questions, right? Or 75, or 103, or ... do you get where I'm coming from?


This is exactly what I am trying to achieve.  If the user does not get the questions right I would like the program to keep producing questions even if the user does not get the certain amount correct ever.

I agree that a while-loop would resonate better with this, however my professor is strictly looking for a for-loop.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zachary Kang wrote:I agree that a while-loop would resonate better with this, however my professor is strictly looking for a for-loop.

Ok, disregarding the fact that I think the requirement is goofy and the resulting code will look unconventional, I'll give you another tip. We don't do people's homework for them around here though so you're going to have to hit the books, refer to your notes and course materials, or hit the online tutorials, or all of the above to figure this out for yourself.

A for-loop header does not need to have any of its parts.  This loop:

In the above example, there is no initialization expression to set the initial value of a loop variable, there is no loop termination condition, and there is no increment expression. So, when execution enters this loop, it will just keep executing the statements inside the loop body over and over. The only way you can exit from this endless loop is to have a break statement in there somewhere.

Now, you can have a for-loop that only has an initialization and termination condition:

This is the kind of for-loop that mimics what a while-loop does.  The initialization and loop termination parts don't have to use only boolean variables but the loop termination condition must be a boolean expression.

So now you're going to need to figure out how to use these tips to solve your problem. This is pretty much all you need to know to solve it.

Good luck.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . simplify your code. . . . avoid asking a question where the answer is a negative number. . . .
An alternative is to use this method. Beware: there are two values for which that method returns the value unchanged. That will probably not be a problem for your current application, however.
 
Zachary Kang
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay that's not a problem.  Thank you for the information!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Junilu Lacar wrote:. . . simplify your code. . . . avoid asking a question where the answer is a negative number. . . .
An alternative is to use this method. Beware: there are two values for which that method returns the value unchanged. That will probably not be a problem for your current application, however.

Since he still wants to display the two numbers as part of the question, they need to be in the right order in order to correctly show something like "What is (bigger) - (smaller)". That method won't help him in this regard.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn! I missed that bit.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!