Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I am lost

 
Brad Page
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am doing a program for my java class midterm. I am lost. I made a quiz using JOpionPane and if/else statements. My problem is I need three other steps in my program:
1. When a quiz is completed i need to choose other topic.
2. repeat a topic.
3. display total number of correct answers for all topics.

I think i need some kind of loop. But i cant figure it out. Thanks for helping (in advance).
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right on about needing a loop. We have several choices for() and while() are probably the most used. for() is good for going through a bunch of things like byte arrays or collections, but not quite what we need here. Let's try while()

Another style we used to call "priming the pump" duplicates a line of code to get rid of the break. I guess I learned this in COBOL because there was no break!

Now I used things like getChoice() in there. I'm implying a method that does your read stuff and returns whatever the user entered. After you get used to it, it's generally easier to understand code if it's broken in smaller chunks. It can be easier to write, too, if you start by writing your intentions and then turn the phrases into methods.
Hope that helped!
[ October 20, 2003: Message edited by: Stan James ]
 
Brad Page
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan! I thought about creating a method too! I wish I was able to view this before I turned in my midterm today. However, I did use the while loop. I got it working great, but, I forgot to end the endless loop(like you said). Hopefully I will not get too many points counted off! I did not think about it until it was too late. I do like your method. That is something that I still need work on. Thanks again!!!
 
Brad Page
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and another thing Stan..
I was told to try to stay away from using the "!" (not equal to). Is that considered a wise thing to do? Or what is your opinion?
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Brad Page:
Oh, and another thing Stan..
I was told to try to stay away from using the "!" (not equal to).

what were the arguments (reasons) for that?
 
Brad Page
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Andres,
My professor just told us to stay away from it. Thats all. I think my professor just doesn't want to confuse us or something.(Since we are beginners)
 
fred rosenberger
lowercase baba
Bartender
Posts: 12081
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we've had discussions at my office about not using "!=". some people feel that "negative logic" is just confusing, and leads to errors in code/debugging.
personally, i feel that it can make code easier to understand sometimes. when you have to go out of your way to avoid using any valid syntax, you really have to question why...
just my 2cents.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've heard arguments about whether you should test for positive conditions or the most likely or the most desired or something else. Readability is pretty subjective.
The ! is small and easy to miss compared to its significance which is rather enormous. That might make some uncomfortable with it. Boolean algebra can get tricky too: eg !(a & b) == (!a | !b) and leads some to write:

I guess the best I can contribute is try to make the code most naturally describe your intentions. And be glad we're not using Perl and saying "do something important UNLESS condition" That still makes my skin crawl.
 
Brad Page
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the best I can contribute is try to make the code most naturally describe your intentions. And be glad we're not using Perl and saying "do something important UNLESS condition" That still makes my skin crawl.

Stan,
Yeah I agree. I start Perl next semester. I guess ill be on here for help then too.
Thanks.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic