Randomly generate 10 numbers, populate an array, then display the array contents with a message box, then calculate the array elements and display the results in a message box

Source: Internet
Author: User

Design ideas:

1. Randomly generate 10 numbers using random, which is stored in the array

2. Use a For loop to store the result in a String object

3. Use the For loop to find all the elements in the array and

4. Using JTextArea and Joptionpane to output the contents of an array

Program Flowchart:

SOURCE program code:

Import javax.swing.*;

public class Initarray {
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);
}
}

Program results:

Randomly generate 10 numbers, populate an array, then display the array contents with a message box, then calculate the array elements and display the results in a message box

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.