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