Array summation and after-class homework

Source: Internet
Author: User

Array summation:

Design ideas:

1. Create an array of type int with a length of 10 to hold the random number;

2. The use of the random class, the generation of stochastic;

3. Use a For loop to deposit random numbers into an array and

4. Generate a message box using JTextArea and Joptionpane, and output the sum of the array

Program Flowchart:

SOURCE program:

 PackageShuzuadd;//Package NameImportjavax.swing.*;//bring all the things inside the swing bag inside the javax bag.  Public classShuzuadd {//class name Public Static voidMain (String args[]) {//Common, static, no return value, function name is main, parameter is array function of type stringString output = "";//define string variable output        intnum=0;//integer variable num        intN[] =New int[10];//call the constructor method to define an object, the parameter is n[10];         for(inti=0;i<n.length;i++) {N[i]=(int) (Math.random () *100+1);//allocate a random number from 1 to 100} output+ = "Randomly generated 10 numbers:" + "\ n";  for(inti=0;i<n.length;i++)//output each randomly generated arrayOutput+=n[i]+ ""; Output+ = "\ n";  for(inti=0;i<n.length;i++)//Array SummationNum+=N[i]; Output+ = "10 numbers added equals:" + "\ n" +num; Joptionpane.showmessagedialog (NULL, output, "outputs", Joptionpane.information_message); }}

After-school assignments:

Reason:

The Java object array cannot be converted to an string[] array, stating that the array to be converted is of type object, but it is wrong to convert it to an array of string classes. When encountering a type conversion error, the first thing to observe is the original type of the object being converted, this need to first convert it into its own type of object, and then according to the object to manipulate the elements inside, again the type of conversion, and sometimes the object of the analysis may have multiple layers of packaging, In the process of conversion requires multiple layers of untie, one layer at a layer to peel off its original type until the final type of the object is obtained, and then convert the type that can not be decomposed into the object of its own target type.

Array summation and after-class homework

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.