[java] assigning values to 8 ints -help-
// 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
hi.
here you go :
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
here you go :
int myArray[];//init array in the main method/constructor/init() or before //usingmyArray = 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
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
(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
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!
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!
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
JP.
==============================================
I feel like a kid in some kind of store...
==============================================
www.thejpsystem.com
//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=(int)(1+ Math.random ()*6);<br> // }<br> <br> //nov 15, 2000<br> // pass array variable y to gran()<br> //need to figure out how arrays work<br> <br> //nov 17, now trying myAray<br> //getting all ZEROS<br> int att[];<br> att=new int[8];<br> myArray(att);<br> System.out.println("strength : " + att[1]);<br> <br> System.out.println("endurance : " + att[2]);<br> System.out.println("intelligence : " + att[3]);<br> System.out.println("leadership : " + att[4]);<br> <br> <br> <br> }}<br> <br> <br> //thx ang GOOD LUCK!<br> <br> <br> </i>
/*
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=(int)(1+ Math.random ()*6);<br> // }<br> <br> //nov 15, 2000<br> // pass array variable y to gran()<br> //need to figure out how arrays work<br> <br> //nov 17, now trying myAray<br> //getting all ZEROS<br> int att[];<br> att=new int[8];<br> myArray(att);<br> System.out.println("strength : " + att[1]);<br> <br> System.out.println("endurance : " + att[2]);<br> System.out.println("intelligence : " + att[3]);<br> System.out.println("leadership : " + att[4]);<br> <br> <br> <br> }}<br> <br> <br> //thx ang GOOD LUCK!<br> <br> <br> </i>
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement