[java] assigning values to 8 ints -help-

Started by
12 comments, last by lupine 23 years, 5 months ago
// 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
"do you like my helmut?"-yoghurt
Advertisement
hi.

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
==============================================I feel like a kid in some kind of store... ============================================== www.thejpsystem.com
Wouldn''t that be...

1 + Math.Random() * 5

Otherwise you could get 7 as a result.
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
"do you like my helmut?"-yoghurt
It was me... I was wrong... 0 >= rand > 1.0 so the original posting was right.
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!
"do you like my helmut?"-yoghurt
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
==============================================I feel like a kid in some kind of store... ============================================== www.thejpsystem.com
darn, ok
let me look at it one more time.
I think it might have to do with scope
"do you like my helmut?"-yoghurt
//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>
"do you like my helmut?"-yoghurt
heeeeeeeeelp
"do you like my helmut?"-yoghurt

This topic is closed to new replies.

Advertisement