Java generates 10 random numbers, fills an array, displays the array contents with a message box, and then sums the output

Source: Internet
Author: User

Java randomly generates A number of numbers, populates an array, and then displays the array contents with a message box , then computes the array elements and displays the results in a message box.

Design ideas: You can use Math.random () *1000 to generate 1000 of the random number, and then into the array, then read the array, output random number, while the addition calculation, and finally all the results in the form of a message box output.

Program Flowchart:

Source:

 Packagesum of random numbers;Importjavax.swing.*; Public classSum { Public Static voidMain (String args[]) {string output= "10 x 1000 or less random number: \ n"; intSum=0; intA []=New int[10];  for(inti = 0;i<10;i++) {A[i]=(int) (Math.random () *1000); Output+= " "+A[i]; Sum+=A[i]; } Output+ = "\ n 10 number of and is:" +sum; Joptionpane.showmessagedialog (NULL, output, "result", Joptionpane.plain_message); }}

Results:

Summary: Using Math.random () *n can generate random numbers in any n, and finally utilize Joptionpane.showmessagedialog (null,output, ""joptionpane.plain_ MESSAGE);
   you can then output the results in a dialog box.

Java generates 10 random numbers, fills an array, displays the array contents with a message box, and then sums the output

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.