AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Little Problem

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
fsd
Guest





PostPosted: Tue Dec 23, 2003 4:05 pm    Post subject: Little Problem Reply with quote



I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA


Back to top
Joseph Dionne
Guest





PostPosted: Tue Dec 23, 2003 4:14 pm    Post subject: Re: Little Problem Reply with quote



fsd wrote:
Quote:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA



Add var1 and var2 together

psuedo code;

switch(var1 + var2)
case 3: three is left
case 4: two is left
case 5: one is left


Back to top
Nicholas Paldino [.NET/C#
Guest





PostPosted: Tue Dec 23, 2003 4:15 pm    Post subject: Re: Little Problem Reply with quote



fsd,

What I would do is store the values in an ArrayList. When you pick the
numbers randomly, remove them from the ArrayList and place into a new
collection. This would require you to change the range of selected random
numbers every time (the first time you pick a random number from the range
1-3, then 1-2, etc, etc). You just have to stop when you have enough
numbers selected. Then, whatever is left over in the ArrayList is what was
not selected.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- [email]mvp (AT) spam (DOT) guard.caspershouse.com[/email]

"fsd" <sg (AT) hotmial (DOT) com> wrote

Quote:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA





Back to top
Dmitriy Lapshin [C# / .NE
Guest





PostPosted: Tue Dec 23, 2003 4:21 pm    Post subject: Re: Little Problem Reply with quote

Something like this (pseudo-code):

var1 = Random(3);
var2 = Random(3);
while (var2 == var1)
{
var2 = Random(3);
}

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"fsd" <sg (AT) hotmial (DOT) com> wrote

Quote:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA




Back to top
Eric Sosman
Guest





PostPosted: Tue Dec 23, 2003 4:22 pm    Post subject: Re: Little Problem Reply with quote

fsd wrote:
Quote:

I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

if (var1 != var2)
System.out.println ( (6 - var1 - var2) + " is missing" );
else
System.out.println ("Two values are missing, you liar!");

--
[email]Eric.Sosman (AT) sun (DOT) com[/email]

Back to top
Marco Martin
Guest





PostPosted: Tue Dec 23, 2003 4:24 pm    Post subject: Re: Little Problem Reply with quote

try this;

int nArrayPos = -1;
for(int x = 0; x < arrayOfVars.Length; x++)
{
if(var1 != arrayOfVars[x] & var2 != arrayOfVars[x])
{
nArrayPos = x;
}
}
if(nArrayPos != 01)
MessageBox.Show(arrayOfVars[x].ToString() + " is not beeing used");


"fsd"
Quote:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA





Back to top
fsd
Guest





PostPosted: Tue Dec 23, 2003 4:32 pm    Post subject: Re: Little Problem Reply with quote

"Sudsy" <bitbucket44 (AT) hotmail (DOT) com> wrote

Quote:
fsd wrote:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA

Think! What do the numbers 1, 2 and 3 add up to? Add up the two
you know, subtract from 6 and you've got the third.

Simple, but highly effective. Well done.

Quote:
Seems obvious now, eh?

It does *now*. :)



Back to top
Marco Martin
Guest





PostPosted: Tue Dec 23, 2003 4:51 pm    Post subject: Re: Little Problem Reply with quote

Sorry, the last if should have been;

if(nArrayPos != -1)

"Marco Martin" <marco_martin (AT) sympatico (DOT) ca> wrote

Quote:
try this;

int nArrayPos = -1;
for(int x = 0; x < arrayOfVars.Length; x++)
{
if(var1 != arrayOfVars[x] & var2 != arrayOfVars[x])
{
nArrayPos = x;
}
}
if(nArrayPos != 01)
MessageBox.Show(arrayOfVars[x].ToString() + " is not beeing used");


"fsd" news:bs9p1f$b81nj$1 (AT) ID-83837 (DOT) news.uni-berlin.de...
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA







Back to top
Brad BARCLAY
Guest





PostPosted: Tue Dec 23, 2003 5:38 pm    Post subject: Re: Little Problem Reply with quote

fsd wrote:
Quote:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

Think more mathematically. Sum the numbers you have together, and
deduct them from the sum of all three numbers. No loops required.

Brad BARCLAY

--
=-=-=-=-=-=-=-=-=
From the OS/2 WARP v4.5 Desktop of Brad BARCLAY.
The jSyncManager Project: http://www.jsyncmanager.org


Back to top
Eric Johannsen
Guest





PostPosted: Tue Dec 23, 2003 5:42 pm    Post subject: Re: Little Problem Reply with quote

Unless I'm missing something, this would not work because theoretically you
could get "1" as your answer every time...and that would be stored in each
element of the array list.

Eric

"Nicholas Paldino [.NET/C# MVP]" <mvp (AT) spam (DOT) guard.caspershouse.com> wrote in
message news:#r#RzAXyDHA.1744 (AT) TK2MSFTNGP12 (DOT) phx.gbl...
Quote:
fsd,

What I would do is store the values in an ArrayList. When you pick
the
numbers randomly, remove them from the ArrayList and place into a new
collection. This would require you to change the range of selected random
numbers every time (the first time you pick a random number from the range
1-3, then 1-2, etc, etc). You just have to stop when you have enough
numbers selected. Then, whatever is left over in the ArrayList is what
was
not selected.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- [email]mvp (AT) spam (DOT) guard.caspershouse.com[/email]

"fsd" <sg (AT) hotmial (DOT) com> wrote in message
news:bs9p1f$b81nj$1 (AT) ID-83837 (DOT) news.uni-berlin.de...
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA






Back to top
Nicholas Paldino [.NET/C#
Guest





PostPosted: Tue Dec 23, 2003 5:58 pm    Post subject: Re: Little Problem Reply with quote

Eric,

That's not what I meant. The array list would be populated with values,
but not randomly. It would be populated with the set of values to select
from (however one comes to those values is irrelevant). Then, a random
number between 0 and the length of the array - 1 is chosen, representing the
index that the value selected is at. That value is placed in another
collection, and the element at that index is removed from the original set.
This reduces the length by one. Even if one is selected every time, it
works because the elements are shifted down one with each removal, providing
a different value.


--
- Nicholas Paldino [.NET/C# MVP]
- [email]mvp (AT) spam (DOT) guard.caspershouse.com[/email]


"Eric Johannsen" <nospam-news (AT) johannsen (DOT) us> wrote

Quote:
Unless I'm missing something, this would not work because theoretically
you
could get "1" as your answer every time...and that would be stored in each
element of the array list.

Eric

"Nicholas Paldino [.NET/C# MVP]" <mvp (AT) spam (DOT) guard.caspershouse.com> wrote
in
message news:#r#RzAXyDHA.1744 (AT) TK2MSFTNGP12 (DOT) phx.gbl...
fsd,

What I would do is store the values in an ArrayList. When you pick
the
numbers randomly, remove them from the ArrayList and place into a new
collection. This would require you to change the range of selected
random
numbers every time (the first time you pick a random number from the
range
1-3, then 1-2, etc, etc). You just have to stop when you have enough
numbers selected. Then, whatever is left over in the ArrayList is what
was
not selected.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- [email]mvp (AT) spam (DOT) guard.caspershouse.com[/email]

"fsd" <sg (AT) hotmial (DOT) com> wrote in message
news:bs9p1f$b81nj$1 (AT) ID-83837 (DOT) news.uni-berlin.de...
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA








Back to top
Alex Hunsley
Guest





PostPosted: Wed Dec 24, 2003 1:12 pm    Post subject: Re: Little Problem Reply with quote

fsd wrote:

Quote:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?

I tried a while loop but I couldn't get it to work...

TIA

You could perhaps use the observation that the final number is equal to:


6 - var1 - var2

alex


Back to top
Walter Mitty
Guest





PostPosted: Sun Dec 28, 2003 3:13 pm    Post subject: Re: Little Problem Reply with quote

"fsd" <sg (AT) hotmial (DOT) com> brightened my day with his incisive wit when in
news:bs9p1f$b81nj$1 (AT) ID-83837 (DOT) news.uni-berlin.de he conjectured that:

Quote:
I have three numbers - 1, 2 & 3.

A randomly selected one of the three are stored in 'var1', and another
randomly selected one is in 'var2'.

How do I find out the number that is left over?


Subtract the two you have from 6.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.