The final result of this article is probably like this. Use Java technology to randomly generate 10 numbers, then fill an array and display the array content in the message box. Then sum the array and output it, and display the result in the message box.
Design idea: You can first use Math.Random()*1000 to generate random numbers within 1000, then store them in the array in sequence, then read the array, output the random numbers, perform addition calculations at the same time, and finally output all results in the form of a message box.
Program flow chart:
source code:
package summation of random numbers;
import javax.swing.*;public class Sum { public static void main(String args[]) { String output= "10 random numbers within 1000 are:/n"; int sum=0; int a []=new int [10]; for(int i = 0;i<10;i++) { a[i]=(int) (Math.random()*1000); output += " "+a[i]; sum += a[i]; } output +="/n/nThe sum of ten numbers is: "+sum; JOptionPane.showMessageDialog(null,output,"result", JOptionPane.PLAIN_MESSAGE ); }}
Result screenshot:
Summary: Use Math.Random()*n to generate random numbers within any n, and finally use JOptionPane.showMessageDialog(null,output," "JOptionPane.PLAIN_MESSAGE);
The results can be output in the dialog box.
The above is the entire content of implementing Java to generate 10 random numbers within 1000 and using a message box to display the contents of the array and then sum the output. I hope you like it.