1, randomly generate 10 numbers, and calculate and
Code:
Importjavax.swing.*; Public classsuijishu{ Public Static voidMain (string[] args) {String output= ""; intnum = 0; intN[] =New int[10]; for(inti = 0;i < n.length;i++) {N[i]= (int) (Math.random () *100+1);//generate random numbers from 1 to 100} output+ = "subscript\tvalue\n"; for(inti = 0;i < n.length;i++) {Output+ = i + "\ T" + n[i] + "\ n"; Num+=N[i]; } JTextArea Outputarea=NewJTextArea (11,10); Outputarea.settext (output); Joptionpane.showmessagedialog (NULL, Outputarea, "Initializing an Suijishu with a Declaration", Joptionpane.information_message); Joptionpane.showmessagedialog (NULL, num, "and of all elements in the array:", Joptionpane.information_message); System.exit (0); }}
Program Flowchart:
Results:
Programming Ideas:
Define an empty array first, then use the Main.random method to generate a random number, insert the number into the string object with a for loop, and finally the For loop calculation.
2, write a program to convert integers into Chinese characters reading method
Code:
Public classnum2rmb{PrivateString[] Hanarr = {"0", "one", "II", "three", "the" , "WU", "Lu", "Qi", "ba", "JIU"}; PrivateString[] Unitarr = {"Ten", "Hundred", "thousand", "Million", "100,000", "Million"}; /*** Turn a four-bit numeric string into a kanji string *@paramNumstr a four-bit numeric string that needs to be converted *@returnA four-bit numeric string is converted into a character string. */ Privatestring Tohanstr (String numstr) {string result= ""; intNumlen =numstr.length (); //iterate through each digit of a numeric string in turn for(inti = 0; i < Numlen; i++ ) { //convert char numbers to int numbers because their ASCII values are exactly//so the char number minus 48 gets the int type number, for example ' 4 ' is converted to 4. intnum = Numstr.charat (i)-48; //if it is not the last digit, and the number is not 0, you need to add units (thousand, Hundred, Ten) if(I! = numLen-1 && num! = 0) {result+ = Hanarr[num] + unitarr[numlen-2-i]; } //Otherwise, do not add units Else { //whether the previous number is "0" and not "0" when added if(Result.length () >0 && hanarr[num].equals ("0") && Result.charat (Result.length ()-1) = = ' 0 ') Continue; Result+=Hanarr[num]; } } //only single digit, direct return if(Result.length () ==1) returnresult; intIndex=result.length ()-1; while(Result.charat (index) = = ' 0 ') {Index--; } if(Index!=result.length ()-1) returnResult.substring (0,index+1); Else { returnresult; } } Public Static voidMain (string[] args) {NUM2RMB nr=NewNUM2RMB (); System.out.println ("Only supports integers (0~ million)"); //test to turn a four-bit numeric string into a Chinese character stringSystem.out.println (Nr.tohanstr ("0")); System.out.println (Nr.tohanstr ("1")); System.out.println (Nr.tohanstr ("10")); System.out.println (Nr.tohanstr ("15")); System.out.println (Nr.tohanstr ("110")); System.out.println (Nr.tohanstr ("123")); System.out.println (Nr.tohanstr ("105")); System.out.println (Nr.tohanstr ("1000")); System.out.println (Nr.tohanstr ("1100")); System.out.println (Nr.tohanstr ("1110")); System.out.println (Nr.tohanstr ("1005")); System.out.println (Nr.tohanstr ("1105")); System.out.println (Nr.tohanstr ("1111")); System.out.println (Nr.tohanstr ("10000")); System.out.println (Nr.tohanstr ("10001")); System.out.println (Nr.tohanstr ("10011")); System.out.println (Nr.tohanstr ("10111")); System.out.println (Nr.tohanstr ("11111")); System.out.println (Nr.tohanstr ("11000")); System.out.println (Nr.tohanstr ("11100")); System.out.println (Nr.tohanstr ("11110")); System.out.println (Nr.tohanstr ("101110")); System.out.println (Nr.tohanstr ("1001110")); }}
Validation results:
3, read the program Whatdoesthisdo.java, explain the function completed by the program.
Public classWHATDOESTHISDO {Static intResult//type int static variable StaticString output; Public Static voidMain (string[] args) {intA[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};//Initialize 10 numbersresult= Whatisthis (A, a.length);//Iterate callsOutput= "Result is:" +result; SYSTEM.OUT.PRINTLN (output); } Public Static intWhatisthis (intB[],intsize) { if(Size = = 1) returnB[0]; Else returnB[SIZE-1] + whatisthis (b, size-1);//The iteration call implements the addition of the last number to the first number }}
Verify:
4, read the program Whatdoesthisdo2.java, explain the function completed by the program.
Code:
Public classWhatDoesThisDo2 { Public Static voidMain (string[] args) {intA[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};//Initialize 10 numbersStringBuilder Sbbuilder=NewStringBuilder ();//Create a StringBuilder objectSomeFunction (A,0, Sbbuilder);//Call function SomefunctionSystem.out.println (sbbuilder);//Output } Public Static voidSomeFunction (intB[],intx, StringBuilder out) { if(X <b.length) {someFunction (b, x+ 1, out);//iterative call function for reverse inputout.append (B[x]+ " ");//after each successive addition } }}
Verify:
Array after class job