The use of Java random numbers and arrays.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.