• Advertisement

Archived

This topic is now archived and is closed to further replies.

[java] assigning values to 8 ints -help-

This topic is 6269 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

// Hello, I am still learning JAva //and I have a question about assigning //a random #(1-6) to eight individual (Int) // I was trying to use a method //like where I declared an array and assigned //then assigned it a[1]=Math.Random... // The first(int) one was assigned //a random # but the rest came up ZERO my question is: I know I could declare and assign each individual (int) Declare (int1) (int1)=Math.random... Declare (int2) (int2)=Math.random and so on but there must be a more elegant or "tight" way to accomplish this in Java. Perhaps a For-loop or perhaps an array method if you have time please post sample code along with your response. Thanks gang

Share this post


Link to post
Share on other sites
Advertisement
hi.

here you go :

        

int myArray[];
//init array in the main method/constructor/init() or before

//using

myArray = new int[8];

for(int i = 0; i < myArray.length; i++) {
myArray[ i ] = (int)(1 + Math.random() * 6);
}


The other numbers where zero 'cause that's what ints are initialized to .

JP.

==============================================
I feel like a kid in some kind of store...
==============================================
www.thejpsystem.com



Edited by - loserkid on November 16, 2000 3:52:48 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Wouldn''t that be...

1 + Math.Random() * 5

Otherwise you could get 7 as a result.

Share this post


Link to post
Share on other sites
JD in the clutch! thats why you wear the "C" JD
(hockey jargon)
no offense Anon but I think you might be off base there

btw Anon is not me!

-----------------------------------------------------------------
elf shot the food!!!!!!!!



Edited by - lupine on November 16, 2000 4:33:53 PM

Share this post


Link to post
Share on other sites
If you will bear with me I need a little
moe help. I am trying to run that code as a method but
I''m getting all zero''s
does this have to do with scope?
I get no error messages.
I can put up my embarrassing code if need be

thx if you can

-----------------------------------------------------------------
elf shot the fooooood!

Share this post


Link to post
Share on other sites
you probably should put up the code, everybody has to start some where....

JP.

==============================================
I feel like a kid in some kind of store...
==============================================
www.thejpsystem.com

Share this post


Link to post
Share on other sites
darn, ok
let me look at it one more time.
I think it might have to do with scope

Share this post


Link to post
Share on other sites
//here it is
/*
this is a program for me to try out
commands and API''s as I learn Java
-robert corrina Nov. 2000-
*/
import java.awt.*;



//my attempt at a method(a.k.a. subroutine)
//this should assign a rondom # to any value
//passed...it works! but only assigns one variable
class TrivialApplication {
//method but outputd only one number
public void gran(int stats[])
{
stats[1]=(int)(1+ Math.random ()*6);}


//as opposed to gran, this method generates
//eight numbers,(the for-loop) I plan to compare
//the two for reference
//***OUTPUT is all zero''s***nov17
static void myArray(int y[]) {
//init array,y, in the main method/constructor/init() //
// or before
//using
y = new int[8];//y is allocated 8 ints
for(int i = 0; i < y.length; i++) {
y = (int)(1 + Math.random() * 6);
}}


//print to screen
static public void main(String argv[]) {
System.out.println( "Hello World!" );
//generate a random number using Math.
//Math. , system. and more are part of
//java.lang which is automatically imported
//-------------------------------------------
//int ranum = (int)(1+ Math.random ()*6);
//
// System.out.println(ranum);
//------------------------------------------

/// static void genstat(ranum[]) {
///
//example of a for loop
// int i;
//// for (i = 0;i < 8; i++)
//System.out.println("Random# =" + (int)(1+ Math.random ()*6))
// ranum[i]=(int)(1+ Math.random ()*6);
// }

//nov 15, 2000
// pass array variable y to gran()
//need to figure out how arrays work

//nov 17, now trying myAray
//getting all ZEROS
int att[];
att=new int[8];
myArray(att);
System.out.println("strength : " + att[1]);

System.out.println("endurance : " + att[2]);
System.out.println("intelligence : " + att[3]);
System.out.println("leadership : " + att[4]);



}}


//thx ang GOOD LUCK!


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
oh I see, you must be familiar with C or some other language like that. I know mainly java but I''m taking C and I was surprised at how strange arrays are in C. So for starters forget all that old stuff. Arrays are objects. Apparently they are implemented differently but from your point of view treat them exactly like all other objects. So if I say a = new int[5] it is like making a class with the following members:

class intArray
{
int 0;
int 1;
int 2;
int 3;
int 4;
} //keeping in mind that you can''t name things 0,1,2,etc..

and instead of writing a.2 you write a[2] ok on to your code

public void gran(int stats[])
{
stats[1]=(int)(1+ Math.random ()*6);
}

ok what you are doing here is getting a reference to an array and returning nothing, but you don''t need to. The first and only line generates a random integer and assigns it to the second variable of stats. The other ones aren''t mentioned in your code so they aren''t altered.

static void myArray(int y[])
{
y = new int[8];//y is allocated 8 ints
//anon: well, not exactly, a new object is made with 8 members
//don''t think of this being low level memory management

for(int i = 0; i < y.length; i++)
{
y = (int)(1 + Math.random() * 6);
}
//does this even compile? It shouldn''t.
//y holds a reference to an object that can hold 8 ints
//y is not the address to y[0] so the only thing that can
//legally be assigned to y is an int[] object, not a primitive int.
//If you want to assign an int to an index you have to assign it to that index.

}

The following should take an array and assign a different random int to each index:

public void assignInts(int[] y)
{
for (int i=0; i{
y=(int)(1 + Math.random()*6);
}
}

not sure if that is what you are asking for.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
oh wait let me try that again

public void assignInts(int[] y)
{
for (int i=0; i{
y=(int)(1 + Math.random()*6);
}
}

Share this post


Link to post
Share on other sites
yes it compiles.
and it runs-i get all zero's-
I can assign variables in
a FOR-loop.
I want a method (or class?)
I can call on from anywhere
and say
"assign every int in this array
a random number(1-6)"
----------------------------------
wait a minute i tried that "public void"
and i got a "cannot call a static/public"
something something


Edited by - lupine on November 22, 2000 8:25:26 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
what??? it is deleting parts of my code, probably yours too. Ok instead of brackets I am going to use B, and I am going to leave the for loop empty, just fill in the standard incrementing system with the i plus plus.

public void assignInts(int[] y)
{
for ()
{
yBiB=(int)(1 + Math.random()*6);
}
}

Share this post


Link to post
Share on other sites

  • Advertisement