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