• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Subtraction test

 
Greenhorn
Posts: 20
Java Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I was trying to make a really simple subtraction test that loops and keeps giving you questions until you get one wrong, I also wanted a counter at the end to tell you how much you got right before you messed up. I tried putting the code in a while loop but it didn't work so I put it in a if loop, here is what I have so far...

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michael Scalzenni wrote:Hello, I was trying to make a really simple subtraction test that loops and keeps giving you questions until you get one wrong, I also wanted a counter at the end to tell you how much you got right before you messed up. I tried putting the code in a while loop but it didn't work so I put it in a if loop, here is what I have so far...


Well, first: WELL DONE for producing a nicely indented piece of code with good names. Thumbs up.

However:
That if statement doesn't really do very much does it? All it really does is work out if your user can read - and get the response to the first (unnecessary) question wrong - so why not get rid of it?

I realise that you might eventually want to make it the basis of a while loop, but right now it's just "noise" - in the words of Shakespeare: "a tale...full of sound and fury, signifying nothing".

It doesn't mean that innermost code is bad though. What's wrong with:?

Now you have a program that (hopefully) deals with one subtraction, and you can test it on that basis.

And when you get it working (it won't right now, BTW), then you can add a while loop if you want.

Do ONE thing at a time. It's the secret to a long and happy life as a programmer.

Winston
 
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will need to declare your variables outside the scope of your application logic, in the body of the main method, so that you can access the variables where ver you need to in the ifs and loop statements.

You also need to have a boolean variable to tell you when the user entered an incorrect answer.

Last tip, its best to use camelCase when naming variables, so something like userAnswer instead of user_answer. Here is a quick and dirty working example using what you had, plus a little modification:

 
Michael Scalzetti
Greenhorn
Posts: 20
Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I added more code to my Subtraction Test so that the answer cannot be negative, so now all I have to do is get it to loop infinite amount of times...
 
Sheriff
Posts: 27451
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I really don't like this line of code, because you don't use either of the variables there inside the loop. However I think you may have made a typing mistake and meant to use this line instead:



But even that isn't very useful, because you don't use the loop variable anywhere and all it says is "Do the contents of this loop once". So it could just as well be left out entirely.

If you want a loop which repeats something forever the easiest way is like this:




You may find you don't really want it to repeat forever, you might want it to stop when some condition happens (like the user learns how to subtract correctly). In that case you should use a boolean variable to keep track of that, like sal jefferson suggested.
 
Michael Scalzetti
Greenhorn
Posts: 20
Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am almost there just need to figure out how to break the loop when the question is wrong...
 
Michael Scalzetti
Greenhorn
Posts: 20
Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:If you want a loop which repeats something forever the easiest way is like this:




You may find you don't really want it to repeat forever, you might want it to stop when some condition happens (like the user learns how to subtract correctly). In that case you should use a boolean variable to keep track of that, like sal jefferson suggested.


I saw your comment to late I had already done this in my code after racking my brain... Probably should pay more attention to my topic :p
 
sal jefferson
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is coming along quite nicely. A few code simplifications if I may?

You have
So you wouldn't need to set it to true again here
instead you could use

And you can remove the last test in the while loop, because you already checked to see it the answer was correct. Again, no need to set testing to true again, it will remain true until you tell it otherwise. So you could change this


To this


Keep it up and I hope this helps!!
 
Michael Scalzetti
Greenhorn
Posts: 20
Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im still having problems breaking the while loop...
This one

Well, this is what I have so far btw...
 
Marshal
Posts: 76863
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure that infinite loops and breaking them are really good programming, but others will disagree. What nobody will disagree with, however, is how bad == true and == false are. Not only are they poor style, because you should write
while (b) ...
or
while (!b) ...
…, but they are error‑prone because you might mistakenly write = instead of ==.
 
sal jefferson
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are still using
Change this to

Also, right here


You are assigning user_answer to the value of Answer. So no matter what the user_answer was, it will be changed to Answer, thus the user will never be wrong and the loop will go on forever.
user_answer = 6;
Answer = 100;
user_answer == Answer;
user_answer = 100;
 
Michael Scalzetti
Greenhorn
Posts: 20
Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sal jefferson wrote:You are still using
Change this to


just tried this and now my code is working the way I want it to, thanks.
 
Michael Scalzetti
Greenhorn
Posts: 20
Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sal jefferson wrote:You are assigning user_answer to the value of Answer. So no matter what the user_answer was, it will be changed to Answer, thus the user will never be wrong and the loop will go on forever.
user_answer = 6;
Answer = 100;
user_answer == Answer;
user_answer = 100;


I was aware of this from the beginning and was careful not to do that, instead I made it so that the user_answer was only = to what the user responded with in the console...
Also here is all of my code if anyone is interested...
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This has already been pointed out, but these two for loops are pointless.
They will both execute once and only once, every time.
If you removed both loops (retaining the code inside them) it will function the same way.
 
Campbell Ritchie
Marshal
Posts: 76863
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sal jefferson wrote:. . . here


You are assigning user_answer to the value of Answer. . . .

No, you aren't. You are using == there to test for identity. In that instance, if you mistakenly wrote = you would suffer a compile‑time error.
 
sal jefferson
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting. I will have to test this to see. Thanks for the correction.

EDIT: You are right, I am sorry for the mistake. Luckily the OP ignored me on that point I dont know what I was thinking here, Its a lot different to look at code on a forum, and be writing code in an IDE.
 
Campbell Ritchie
Marshal
Posts: 76863
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sal jefferson wrote:. . .
So you wouldn't need to set it to true again here
. . .

The OP is not trying to reset testing; it is a mistake while trying to write == true. We see that sort of error every now and again.
 
Campbell Ritchie
Marshal
Posts: 76863
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two common and opposite errors with == and = inside (). I have made those errors many times myself. You only see them after keywords like while and if.
You can write == which works for everything and returns a boolean. It will always compile and usually return the “wrong” result because people use it inappropriately.
Then you can write if (b = x) …. Unless both operands of the = operator are boolean type, it will fail to compile in such circumstances. I remember getting compiler errors on the lines of

Expected boolean, found int

…or similar becuse I had intended to write ==. The only time you can get that sort of thing to compile (as before) is with two booleans, in which case you get two logic errors for the price of one.

I shall have to think of some alternatives to writing if (b1 == b2) ... or similar. What about
if (!b1 ^ b2) ...
if (!(b1 ^ b2)) ...
if (b1 ^ !b2) ...
?
I can't be bothered to work out truth tables now; I shall leave it to you. I think you will find each of those expressions equivalent to if (b1 == b2) ... but avoiding the risks inherent in the == operator.


The assignment operator shouldn't be =, but :=, in which case = can be used for equality and there would be less risk of such confusion. But we are stuck with = for assignment.
 
Campbell Ritchie
Marshal
Posts: 76863
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A few minuste ago, I wrote:. . . It will always compile . . ..

And I was mistaken; a lot of primitives are incompatible with other types and x == y can fail to compile.
 
There are no more "hours", it's centi-days. They say it's better, but this tiny ad says it's stupid:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic