Generating random Operators
Im trying to generate three(3) random operators at once,using a method like this:
public randOperators{
Random randOps =new Random();
char[] chars =newchar[]{'+','-','*'.'/'};
}
Thats as far as i can go,for now. Not sure,if i can use a for loop,or what works best. Thanks for any help.
[709 byte] By [
exl5a] at [2007-11-27 9:09:46]

randOps.nextInt(int n) will give you and integer between 0 and n-1, inclusive. You can use that as an index into the array, and get the operator at that index.
jverda at 2007-7-12 21:50:20 >

Now,if I want to produce,3 operators,not one,will a for loop do it? or any suggestions?
public randOperators{
Random randOps = new Random();
char[] chars = new char[]{'+','-','*'.'/'};
randOps.nextInt(4);
}
exl5a at 2007-7-12 21:50:20 >

> Now,if I want to produce,3 operators,not one,will a
> for loop do it?
I don't think you need to ask that question.
You know what a for loop is for, right?
You know how to do something multiple times, right?
You do have the ability to try something and see if it works, right?
jverda at 2007-7-12 21:50:20 >

yes,to all of the above.Ive tried it many times,but so far im not successful yet at it. I have the ideas,the concepts,but when I try to put it all together,I get confused
exl5a at 2007-7-12 21:50:20 >

So post your best effort and details about how it didn't work for you.
jverda at 2007-7-12 21:50:20 >

Here's what ive done,but no success so far.
-For loop to loop 3 times to get me 3 ops
-The index to get me an operator at that index
public character randOperators{
Random randOps = new Random();
for( int i =0; i<= 3; i++ ){
char[] chars = new char[]{'+','-','*'.'/'};
randOps.nextInt(3);
return randOps.nextInt[i];
}
}
exl5a at 2007-7-12 21:50:20 >

so you don't know about for loops. read up on them and return statements. also think about where you are initiating your array. finally, how do you want to output the three resulting operators?
I do need to read more on return statement,for example,Im not sure what to return the operators as: string,character,but I would think anyone of those would be just fine. I want it returned just like this: + or -.
exl5a at 2007-7-12 21:50:20 >

> Here's what ive done,but no success so far.
> -For loop to loop 3 times to get me 3 ops
In general it helps if you're more specfic about HOW it's not working.
Your syntax is all messed up, for one thing. You need to pay closer attention to that.
* It's char, not character.
* In a method decaration, after the method name, you need parens, either empty or with the list of args and their types.
In addition to that, think about this for a second:
for ( ... ) {
do stuff
return something
}
As soon as return is called, the method ends. You can't return multiple times from one method.
Stop and think about it for a second. If you're calling that method char ch = randOperators();
you'll get a single character back.
Either call that method three times, and move the loop out of the method, or have that method return an array of char.
> -The index to get me an operator at that index
So, you don't know how to get the element at a particular index in an array? If I say foo is a char[], now get me foo's 3rd element, you don't know how to do that?
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
jverda at 2007-7-12 21:50:20 >

Thank you.Very helpful.foo[] is array of char and u want the third element,which is index = 2you can call the charAt method to get the third element of foo[].
exl5a at 2007-7-12 21:50:20 >

> Thank you.Very helpful.
> foo[] is array of char and u want the third
> element,which is index = 2
> you can call the charAt method to get the third
> element of foo[].
No, you can't.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
jverda at 2007-7-12 21:50:20 >

Ok,great my bad.I go read a bit arrays,obviously thats why im having problems. Thanks anyway.
exl5a at 2007-7-12 21:50:20 >

> Ok,great my bad.I go read a bit arrays,obviously
> thats why im having problems.
Good call. :-)
Once you've done that, post what you think will get the Nth element from an array--or better yet, write code that does that--and only that. If it works, great, and if not, post the next question here.
Then move onto the next piece.
jverda at 2007-7-12 21:50:20 >

what gets the Nth element of an array called Foo[] is Foo index[n-1].Still I do need to read on that..for my own betterment :-)
exl5a at 2007-7-21 22:55:59 >

> what gets the Nth element of an array called Foo[] is> Foo index[n-1].Yup. That is, if the element type is Foo and the variable name is index.
jverda at 2007-7-21 22:55:59 >

> Im trying to generate three(3) random operators at> once,using a method like this:Do it one at a time.
Good idea.
exl5a at 2007-7-21 22:55:59 >

> what gets the Nth element of an array called Foo[] is> Foo index[n-1].> > Still I do need to read on that..for my own> betterment :-)In Java the elements are indexed from 0 to N-1 where N is the size of the array. It's just the way it
> > what gets the Nth element of an array called Foo[]
> is
> > Foo index[n-1].
> >
> > Still I do need to read on that..for my own
> > betterment :-)
>
> In Java the elements are indexed from 0 to N-1 where
> N is the size of the array. It's just the way it is.
From his post, it seems he understands that.
jverda at 2007-7-21 22:55:59 >

> From his post, it seems he understands that.Are you guessing or do you know?
> > From his post, it seems he understands that.
>
> Are you guessing or do you know?
It's an educated guess, with an exceedingly high probability of being right. Read his post closely. He simply states correctly--in response to me posing the question earlier--that you access the Nth element with [N-1].
jverda at 2007-7-21 22:55:59 >

> > > From his post, it seems he understands that.
> >
> > Are you guessing or do you know?
>
> It's an educated guess, with an exceedingly high
> probability of being right. Read his post closely. He
> simply states correctly--in response to me posing the
> question earlier--that you access the Nth element
> with [N-1].
Well I read his reply 15 and somehow got the idea this was the problem but if you know it isn't then of course at least you know it isn't.
Jverd is absolutely correct,as he almost always is.Judjei...youre losing this one.Let it go.Read the posts you will understand.
exl5a at 2007-7-21 22:55:59 >

> Jverd is absolutely correct,as he almost always is.
I wouldn't go that far. Still, maybe you could mention it to my wife...
:-)
> Judjei...youre losing this one.
Ooooh! I didn't realize it was a competition. What do I win? What do I win? GIMME GIMME GIMME!!!
jverda at 2007-7-21 22:55:59 >

> Jverd is absolutely correct,as he almost always is.> > Judjei...youre losing this one.Let it go.Read the> posts you will understand.Well, what's important is that you feel your question has been answered. -.)
Welcome back, UJ.
jverda at 2007-7-21 22:55:59 >

> Judjei...youre losing this one.> > Ooooh! I didn't realize it was a competition. What do> I win? What do I win? GIMME GIMME GIMME!!!Well, for what it's wort, I can offer you a kiss.
No,not a competition at all.I left some room for improvement for him 'almost' so he's not totally there yet :-)Now it seems like youre coming around,joining us..on the same page..Youre right too,I feel like my question been answered.
exl5a at 2007-7-21 22:55:59 >

> ...> Well, for what it's wort, I can offer you a kiss.C# made you soft!; )
> Welcome back, UJ.Thank you. I managed to stay away for quite a while this time. -:)
> > ...> > Well, for what it's wort, I can offer you a kiss.> > C# made you soft!> ; )No C# would be a piece of cake, it's C++/CLI actually. I'm humble now -:)
> ...> No C# would be a piece of cake, it's C++/CLI actually.Oh, I thought you mentioned you were going to do a large scale C# project a while back.> I'm humble now -:)Yeah, right!Ah well, nice to see you around here again.; )
> Oh, I thought you mentioned you were going to do a
> large scale C# project a while back.
Yes sure but it turned out C++/CLI was better so here I am in the middle of it.
C# is 100% Microsoft and the client didn't want that so they went for classic C++ with some CLI and there I am.
> Ah well, nice to see you around here again.Are you insane?
> > Ah well, nice to see you around here again.> > Are you insane?Perhaps. But when she's not "arguing" with other forum members, I value reading what she has to say about Java: she IS a good developer. The place is a bit dull without her, I guess.