The use of Java random numbers and arrays.
One: Topic
Two codes:
public class Students {
int number; School Number
int state; Grade
int score; Results
public Students (int number, int. State, int score) {
Super ();
This.number = number;
state = State;
Score = score;
}
public static void Main (string[] args)
{
Students Sds[]=new students[20];
for (int i=0;i<sds.length;i++)
{
Sds[i]=new Students (i+1, (int) (Math.random () *3+1), (int) (Math.random () *100));
if ((int) (Math.random () *3+1) ==3)
{
SYSTEM.OUT.PRINTLN ("Student number is:" + (i+1) + ", Grade:" + 3
+ ", Score:" + (int) (Math.random () *100));
}
}
To find the value of number
for (int i=0;i<20;i++)
{
System.out.println (Sds[i]); This is an empty array, so the result of the output is null
}
For the use of random numbers, the grade requirement is between
for (int i=0;i<20;i++)
{
Random number Math.random (), the number produced is between 0~1.
SYSTEM.OUT.PRINTLN ((int) (Math.random () *3+1));
}
Seeking results
/*
for (int i=0;i<20;i++)
{
SYSTEM.OUT.PRINTLN ((int) (Math.random () *100));
}
*/
}
}
Three: Analysis
1. The first consideration is how to create an array, which can be a reference data and a basic type of data. And here is the reference data type. Consider the construction method when it comes to reference data types. The basic data is not the same.
2. Is the application of random numbers. The Math.random () is usually used
For (int i=0;i<20;i++)
{
Random number Math.random (), the number produced is between 0~1.
SYSTEM.OUT.PRINTLN ((int) (Math.random () *3+1));
}
Note: When using random number math.random, the resulting data is between 0~1, if the first data needs to be changed to Math.random ().
for (int i=0;i<20;i++)
{
System.out.println (Sds[i].number);
}
The code will be the wrong way. The wrong place
There will be no pointers.
Students sds[]=new students[20]; is an array created based on the student class Students , not an array of basic data types. So the error will be.
The use of Java random numbers and arrays.