Generate variables in java.

Paper Cars

Audio Production Services
Supreme
Feedback score
8
Posts
514
Reactions
103
Resources
0
Heyo Java Developers!

Im not a great java developer and my knowledge of the language is very limited.

I have a question.

lets say I have a method to generate a random number, randNum(int min, int max);,
and I want to generate a random number of random numbers, and store each number to a different variable.

For instance,


Code:
int x = randNum(0, 4);

for(int y = 0; y < x; y++){

  randNum(0,9);

    //If int x = 0 it will not run,
    // If int x = 1 it will generate a random number, and store it to variable 'num1'
    //If int x = 2 it will generate a random number and store it to variable 'num1', then generate another random number and store it to variable 'num2', and so on until y > x
}

Is this possible? Whats the best way to do this?
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

Paper Cars

Audio Production Services
Supreme
Feedback score
8
Posts
514
Reactions
103
Resources
0
huh ? can you explain better
I shall try.

I want to make a loop.

I want this loop to run x times. Lets say 2. I want the loop to run twice.

Each time the loop runs, it will generate 1 random number.
I want it to store this random number to a variable, but I want each random number to be stored in a separate variable.

For example the loop will run twice,
So it will generate two random numbers.
I want it to store the first random number to variable A.
I want it to store the second random number to Variable B.

If the loop runs four times, which will generate 4 random numbers,
I want it to store the first random number to Variable A, the second number to variable B, the third random number to Variable C, and the fourth random number to variable D.
 

Inviss

Management / Head of Development @ AkumaMC
Supreme
Feedback score
21
Posts
734
Reactions
267
Resources
3
I shall try.

I want to make a loop.

I want this loop to run x times. Lets say 2. I want the loop to run twice.

Each time the loop runs, it will generate 1 random number.
I want it to store this random number to a variable, but I want each random number to be stored in a separate variable.

For example the loop will run twice,
So it will generate two random numbers.
I want it to store the first random number to variable A.
I want it to store the second random number to Variable B.

If the loop runs four times, which will generate 4 random numbers,
I want it to store the first random number to Variable A, the second number to variable B, the third random number to Variable C, and the fourth random number to variable D.
I may have just misunderstood what you're asking for here, but without declaring loads and loads of variables I don't think there is feasible way to do this without doing the classic java trick... throwing a map at it.

If you store each thing in a map or a list, you can just access it using the key (or the index in a list). This is a VERY rough example and will absolutely not work if you try to compile it

Code:
final Map<String, Integer> numberMap = ...

for (int x = 0; x < timesToRun; x++) {
  this.numberMap.put(variableName, random);
}

You can then just access it from the map using numberMap#get(key), or just get a collection of the values using #values.

Hope I understood what you meant.
 

Paper Cars

Audio Production Services
Supreme
Feedback score
8
Posts
514
Reactions
103
Resources
0
I may have just misunderstood what you're asking for here, but without declaring loads and loads of variables I don't think there is feasible way to do this without doing the classic java trick... throwing a map at it.

If you store each thing in a map or a list, you can just access it using the key (or the index in a list). This is a VERY rough example and will absolutely not work if you try to compile it

Code:
final Map<String, Integer> numberMap = ...

for (int x = 0; x < timesToRun; x++) {
  this.numberMap.put(variableName, random);
}

You can then just access it from the map using numberMap#get(key), or just get a collection of the values using #values.

Hope I understood what you meant.

That will do exactly what I want! Thanks! I'm in an intro java class, so Im still learning about the language. I'm sitting here constantly like I want to do this but I have no idea how to do that...
 

Inviss

Management / Head of Development @ AkumaMC
Supreme
Feedback score
21
Posts
734
Reactions
267
Resources
3
That will do exactly what I want! Thanks! I'm in an intro java class, so Im still learning about the language. I'm sitting here constantly like I want to do this but I have no idea how to do that...
Glad it helped, let me know if you've got any other questions!
 
Top