Array-Random number

Source: Internet
Author: User

Requirements: Randomly generate 10, populate an array, then display the array contents with a message box, then calculate the array element's and the result is also displayed in the message box.

Design ideas: Randomly generate 10 numbers using random, use a for loop to store the results in a string object, using JTextArea and Joptionpane to output the contents of the array

Program Flowchart

Source:

Import javax.swing.*;

public class Lianxi {public static void main (string args[]) {string output = ""; int num=0;

int n[] = new INT[10]; for (int i=0;i<n.length;i++) {n[i]= (int) (Math.random () *100+1);}

Output + = "subscript\tvalue\n";

for (int i = 0; i < n.length; i++) {output + = i + "\ t" + n[i] + "\ n"; num+=n[i];}

JTextArea Outputarea = new JTextArea (11, 10); Outputarea.settext (output);

Joptionpane.showmessagedialog (NULL, Outputarea, "Initializing an Array with a Declaration", Joptionpane.information_ MESSAGE); Joptionpane.showmessagedialog (Null,num, "the and of all elements in the array:", joptionpane.information_message);

System.exit (0); } }

Results:

Array-Random number

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.