Jump to content

  • Log In with Google      Sign In   
  • Create Account


[java] assigning values to 8 ints -help-


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 lupine   Members   -  Reputation: 122

Like
Likes
Like

Posted 16 November 2000 - 08:30 AM

// 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

Sponsor:

#2 loserkid   Members   -  Reputation: 122

Like
Likes
Like

Posted 16 November 2000 - 08:50 AM

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

#3 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 16 November 2000 - 09:02 AM

Wouldn''t that be...

1 + Math.Random() * 5

Otherwise you could get 7 as a result.

#4 lupine   Members   -  Reputation: 122

Like
Likes
Like

Posted 16 November 2000 - 09:31 AM

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

#5 snowmoon   Members   -  Reputation: 122

Like
Likes
Like

Posted 16 November 2000 - 09:43 AM

It was me... I was wrong... 0 >= rand > 1.0 so the original posting was right.

#6 lupine   Members   -  Reputation: 122

Like
Likes
Like

Posted 17 November 2000 - 01:32 PM

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!

#7 loserkid   Members   -  Reputation: 122

Like
Likes
Like

Posted 17 November 2000 - 02:13 PM

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

#8 lupine   Members   -  Reputation: 122

Like
Likes
Like

Posted 19 November 2000 - 09:23 AM

darn, ok
let me look at it one more time.
I think it might have to do with scope

#9 lupine   Members   -  Reputation: 122

Like
Likes
Like

Posted 21 November 2000 - 06:12 AM

//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!




#10 lupine   Members   -  Reputation: 122

Like
Likes
Like

Posted 22 November 2000 - 08:01 AM

heeeeeeeeelp

#11 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 22 November 2000 - 10:31 AM

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.


#12 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 22 November 2000 - 01:20 PM

oh wait let me try that again

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



#13 lupine   Members   -  Reputation: 122

Like
Likes
Like

Posted 22 November 2000 - 01:21 PM

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

#14 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 22 November 2000 - 01:22 PM

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);
}
}






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS