Random numbers are stored in arrays and summed

Source: Internet
Author: User

First, the appearance of Ljava.lang.Object; What is the reason for @ba8a1dc? What does that mean?

A: The problem occurs when the SQL statement executes a query with only one column, and when there are multiple columns, there is no problem with the array, if only one column defaults to String or another type. Type conversion errors can only be strongly converted to Object, and then values are added to the ToArray () method by adding a String array and then outputting.

Second, the program will generate a number of random numbers into the array and sum

Design Idea : Import The Random class, generate stochastic number, import joptionpane class, use message box, output information; create int Type array and set the length of ten, used to store random numbers;random random=new random (); Create a random object in the for Call in the loop, generate A random number,r[i]=random.nextint (), and the final sum of the output results.

Program Flowchart:

Source:

Package com;

Import java.util.random;//Importing the Random class

Import javax.swing.joptionpane;//Importing Joptionpane class

public class Random_ {

public static void Main (string[] args) {

int r[]=new int[10]; Creates a new array object of type int, with a length of 10

The initialization of int s=0;//

Random random=new random ();//Create Random Object

for (int i=0;i<10;i++)//Use a loop to generate 10 random numbers and deposit them in the array

{

R[i]=random.nextint ();//Generate random numbers

s+=r[i];//summation

}

Joptionpane.showmessagedialog (

NULL, "The contents of the array are:" +r[0]+ "" +r[1]+ "" +r[2]+ "" + "" +r[3]+ "" +r[4]+ "" +r[5]+ "" +r[6]+ "" +r[7]+ "" +r[8]+ "" +r[9 ");

Output an array element using a message box

Joptionpane.showmessagedialog (

NULL, "The and of the elements of the array is:" +s);

Use a message box to output the array's and

}

}

Results:

Programming Summary: There are two ways to generate random numbers, I use this one more flexible, there is no upper and lower limit but you can add the upper limit, you need to create a random object, another can be limited; Create an array object when creating a new array to open up space.

Random numbers are stored in arrays and summed

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.