Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Java 2
This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 3rd, 2003, 05:54 PM
Authorized User
 
Join Date: Aug 2003
Location: Lima, Ohio, USA.
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to endamist Send a message via Yahoo to endamist
Default loops and testing multiple integers

I'm trying to do the end of the chapter exercises and made this program to produce six random numbers between 1 to 49 five times.


here is the program
************************************************** **************

public class LotteryNumbers {
        public static void main(String[] args) {
             int count = 6;
             while(--count > 0) {
             int a = 1+(int)(49*Math.random());
             int b = 1+(int)(49*Math.random());
             int c = 1+(int)(49*Math.random());
             int d = 1+(int)(49*Math.random());
             int e = 1+(int)(49*Math.random());
             int f = 1+(int)(49*Math.random());
 System.out.println(a + ", " + b + ", " + c + ", " + e + ", " + f);
           }
        }
   }


************************************************** ****************

This produces an output of 6 numbers on each of 5 lines. the problem is trying to test each integer after "a" to be sure that they do not match any of the integers that proceed them, so that no two numbers on each line or in each excecution of the loop do not match eachother.

The books gives examples of testing two diffrent variables with &, &&, | and ||. I wanted to use nested loops for variables b-f that would test for matches of all proceeding variables for matches and if it was true to repeat the process of producing a random number from 1 to 49 until the test was false.

The test for b only required to be tested against a. The test for c required testing against a and b. When I got to d there were now three variables to test against and this is where I run into a problem since the only knowledge I have so far is from chapters 1-3.
e would have to be tested against 4 variables and f against 5 variables.

I'm still not sure if I can do nested loops for each of the variables from b-f but that is the least of the problem. so,
while I play around with other ideas to write this program to get the results I want does anyone else have any suggestions or helpful information?

Thanks
Endamist

  #2 (permalink)  
Old August 3rd, 2003, 07:34 PM
Authorized User
 
Join Date: Aug 2003
Location: Lima, Ohio, USA.
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to endamist Send a message via Yahoo to endamist
Default

Oh I can't believe I got that without any help!!! :D

fragment of changes:
************************************************** ******

        int count = 6;
        while(--count > 0) {
        int a = 1+(int)(49*Math.random());
        int b = 0;
        int c = 0;
        int d = 0;
        int e = 0;
        int f = 0;
            do {
               int bet = 1+(int)(49*Math.random());
               b = bet;
            }while(b == a);

            do {
               int cas = 1+(int)(49*Math.random());
               c = cas;
            } while(c == b | c == a);

            do {
                int dec = 1+(int)(49*Math.random());
                d = dec;
            } while(d == a | d == b | d == c);
************************************************** *******************

turns out I was having the most trouble with trying to write a test that would prevent any of the six numbers for one set from producing the same random number because none of the examples I'd seen so far delt with more than two variables outside of the one you were testing. and had to remember that you had to declare an integer outside of the do/while loops because if you declare them only inside the do/while loop blocks they don't exist outside of that and would not print the string I wanted to output. But I also had to declare a diffren variable inside the loop blocks and then reset the value of the original variables to the one inside the loop block because if I simply used f it would not compile and kept telling me that f was already declared.

okay so noone else is impressed but I'd say it's pretty darn good for someone who hasn't been to school in over 10 year and is teaching herself. and who knows maybe what I write here will help someone else who is doing the same or some young person who is just starting out.

Endamist

  #3 (permalink)  
Old August 3rd, 2003, 07:42 PM
Authorized User
 
Join Date: Aug 2003
Location: Lima, Ohio, USA.
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to endamist Send a message via Yahoo to endamist
Default

bty, I wonder if it would be possible to get more examples of multiple variable test... code examples added to the next edition or at least a web page for the book where authors/publishers could share more examples that they couldn't fit into the book. I know that this forum somewhat a web page for the book but a page of FAQ, and extras that the author/publishers wanted to add would make a book all the better because not only could you list corrections to errors but you could touch on the most often asked questions about topics in the book. ;)

just a thought.

endamist

  #4 (permalink)  
Old August 7th, 2003, 01:36 PM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Don't know about the others but I'm impressed with your solution:)
There were two objectives with this particular example: first to make you struggle with all those nested loops that you need here to deal with the problem of duplicate values for a,b,c etc. , and second so that you would really appreciate arrays when you get to them in the next chapter.
  #5 (permalink)  
Old August 12th, 2003, 03:04 PM
Authorized User
 
Join Date: Aug 2003
Location: Lima, Ohio, USA.
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to endamist Send a message via Yahoo to endamist
Default

Thank goodness there is a better why of doing it. well... guess I'll get there soon enough to learn about arrays. Still trying to figure out the last exersize. Testing for vowels and lower case to produce a random capital letter with no vowels. Grrrrr... There has got to be something I'm over looking.

Well back to beating my head gainst the wall till I figure it out.

Endamist

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
break for-each loops, or limit amount of loops warhero XSLT 2 July 4th, 2007 02:18 AM
Unsigned integers AndrewH Java Basics 1 September 26th, 2006 02:46 PM
While loops and For loops in XSLT spencer.clark XSLT 1 August 5th, 2005 09:50 AM
Using integers with text boxes clojinted General .NET 1 February 19th, 2005 08:12 PM
Sum of All Integers Between 2 numbers bicho Classic ASP Basics 4 July 6th, 2003 01:01 PM



All times are GMT -4. The time now is 06:07 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.