This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes I am lost Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I am lost" Watch "I am lost" New topic
Author

I am lost

Brad Page
Greenhorn

Joined: Oct 13, 2003
Posts: 9
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

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Brad Page
Greenhorn

Joined: Oct 13, 2003
Posts: 9
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

Joined: Oct 13, 2003
Posts: 9
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

Joined: Nov 27, 2001
Posts: 1561
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?


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Brad Page
Greenhorn

Joined: Oct 13, 2003
Posts: 9
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

Joined: Oct 02, 2003
Posts: 11170
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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

Joined: Oct 13, 2003
Posts: 9
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I am lost
 
Similar Threads
Methods
DataOutputStream object might not have been initialized????
can some one help me im just new in java.
How to Save data about 5 computers?
passing arrays between claasses