java06-array hands-on brain

Source: Internet
Author: User

1. Read the Qipan.java sample program to learn how to draw a Gobang disk using two-dimensional arrays and circular statements.

Defines a private two-dimensional array as a chessboard. and defines the length. The symbol is then printed to connect it as a chessboard in the console display. Establishes the buffer used to read the input coordinates. Some methods are used to split the input coordinates and re-assign the elements of the array.

1 ImportJava.io.*;2 3  Public classQipan4 {5     //define a two-dimensional array to act as a chessboard6     Privatestring[][] board;7     //define the size of the board8     Private Static intBoard_size = 15;9      Public voidInitboard ()Ten     { One         //initializing an array of checkers Aboard =NewString[board_size][board_size]; -         //assign each element "╋" to draw a chessboard on the console -          for(inti = 0; i < board_size; i++) the         { -              for(intj = 0; J < Board_size; J + +) -             { -BOARD[I][J] = "╋"; +             } -         } +     } A     //How to output a checkerboard in the console at      Public voidPrintboard () -     { -         //Print each array element -          for(inti = 0; i < board_size; i++) -         { -              for(intj = 0; J < Board_size; J + +) in             { -                 //print array elements without wrapping to System.out.print (Board[i][j]); +             } -             //outputs a newline character after each line of array elements is printed theSystem.out.print ("\ n"); *         } $     }Panax Notoginseng      Public Static voidMain (string[] args)throwsException -     { theQipan GB =NewQipan (); + Gb.initboard (); A Gb.printboard (); the         //This is the method for getting keyboard input +BufferedReader br =NewBufferedReader (NewInputStreamReader (system.in)); -String Inputstr =NULL; $System.out.println ("Please enter the coordinates of your chess, should be in the format x, y:"); $         //Br.readline (): Whenever you enter a line on the keyboard press ENTER, the content you just entered will be read by BR.  -          while((Inputstr = Br.readline ())! =NULL) -         { the             //separates the user-entered string into 2 strings with a comma (,) as a delimiter -string[] Posstrarr = Inputstr.split (",");Wuyi             //converts 2 strings to the coordinates of a user's chess the             intXPos = Integer.parseint (posstrarr[0]); -             intYPos = Integer.parseint (posstrarr[1]); Wu             //assign the corresponding array element as "".  -GB.BOARD[XPOS-1][YPOS-1] = "";  About             /* $ The computer randomly generates 2 integers, which are assigned to the board array as the coordinates of a computer's chess game.  - also involves - 1. The validity of the coordinates, can only be a number, not beyond the board range - 2. If the point of chess under, can not repeat chess.  A 3. After each play, you need to scan who wins . +              */ the Gb.printboard (); -System.out.println ("Please enter the coordinates of your chess, should be in the format x, y:"); $         } the     } the}

2. Write a program to convert an integer to a kanji reading string. For example, "1123" is converted to "1123". Further, can you change the amount represented in the numbers to "Chinese character expression?" For example, the "¥123.52" converted to "one Bai San Yuan Wu angle of the three points."

1  Public classchange{2     3     PrivateString[] Hanarr = {"0", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};4     PrivateString[] Unitarr = {"Ten", "Hundred", "thousand", "million"};5     Privatestring Tohanstr (string numstr)6     {7String result = "";8         intNumlen =numstr.length ();9         Ten          for(inti = 0; i < Numlen; i++)//converting characters into shaped numbers One         { A             intnum = Numstr.charat (i)-48; -  -             if(I! = numLen-1 && num! = 0) the             { -Result + = Hanarr[num] + unitarr[numlen-2-i];//add units before the last digit -             } -  +             Else    //no units after the lowest number -             { +  A                 if(Result.length () >0 && hanarr[num].equals ("0") && Result.charat (Result.length ()-1) = = ' 0 ') at                 Continue; -Result + =Hanarr[num]; -             } -          } -  -          if(Result.length () ==1) in               -              returnresult; to  +          intIndex=result.length ()-1; -           while(Result.charat (index) = = ' 0 '){ theindex--; *          } $          if(Index!=result.length ()-1)Panax Notoginseng               returnResult.substring (0,index+1); -          Else { the              returnresult; +          } A      } the  +       Public Static voidMain (string[] args) { -Change m =NewChange (); $System.out.println (M.tohanstr ("142")); $  -      } -}

3. The previous introduction of the JDK provided by the BigInteger can complete a large number of calculations, if not use it, directly using the array to express large numbers, you can achieve the same function?

BigInteger is an immutable integer of arbitrary precision. In all operations, BigInteger (such as the basic integer type of Java) are represented in twos complement form. BigInteger provides a counterpart to all Java's basic integer operators and provides all the relevant methods of Java.lang.Math. In addition, BigInteger provides the following operations: modulo arithmetic, GCD calculation, prime number test, prime generation, bit manipulation, and some other operations.

Converts a byte array containing BigInteger's binary complement representation to BigInteger. The input array is set to Big-endian byte order: The most significant byte is in the 0th element.

4. Randomly generate 10 numbers, populate an array, then display the contents of the array with a message box, then calculate the array elements and display the results in a message box.

Idea: Use random methods to generate random numbers automatically, and define an array of length 10. The generated random number is stored in the array with a For loop, and the random number is set to a double-digit number (a problem occurs when the null parameter is present). The definition variable is used to store the content to be output in the dialog box, which is the resulting 10 random number. continuously accumulate and output.

1      Public Static voidMain (String args[]) {2     3         inta[]=New int[10];4         intSum=0;5Random rand=NewRandom ();6String output= "";7     8          for(inti=0;i<10;i++){9 TenA[i]=rand.nextint (100); Onesum+=A[i]; AOutput+=string.valueof (A[i]) + ""; -           -         } the  -Joptionpane.showmessagedialog (NULL, output, "random number is" + "", joptionpane.plain_message); -Joptionpane.showmessagedialog (NULL, SUM, "The sum is" + "", joptionpane.plain_message); -System.exit (0);  +  -        } +  A}

Summary: Always report that there are no errors to initialize the variables, and that the generation of random numbers is not "random enough"

java06-array hands-on brain

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.