Java hands-on brain

Source: Internet
Author: User

I. randomly generate 10 numbers, populate an array, then display the contents of the array with a message box, and then calculate the array elements and the results are also displayed in the message box.

1. Programming Ideas:

2. Program Flowchart:

3. Program Source code:

Import Java.util.Random;

Import Javax.swing.JOptionPane;

public class Suijishu {

public static void Main (string[] args) {

String output= "The random number within the range of ten is:\ n";

int sum=0;

int s[] = new INT[10];

Random n = new random ();

for (int i=0;i<=9;i++)

{

S[i]=n.nextint (101);

Output + = "" +s[i];

Sum + = S[i];

}

Output + = "\ n 10 number of the and is:" +sum;

Joptionpane.showmessagedialog (Null,output, " results ",

Joptionpane.plain_message);

}

}

4. Procedure:

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

1. Code:


Import java.io.*;

public class Qipan
{
Define a two-dimensional array to act as a chessboard
Private string[][] board;
Define the size of the board
private static int board_size = 15;
public void Initboard ()
{
Initializing an array of checkers
board = new String[board_size][board_size];
Assign each element "╋" to draw a chessboard on the console
for (int i = 0; i < board_size; i++)
{
for (int j = 0; J < Board_size; J + +)
{
BOARD[I][J] = "╋";
}
}
}
How to output a checkerboard in the console
public void Printboard ()
{
Print each array element
for (int i = 0; i < board_size; i++)
{
for (int j = 0; J < Board_size; J + +)
{
Print array elements without wrapping
System.out.print (Board[i][j]);
}
Outputs a newline character after each line of array elements is printed
System.out.print ("\ n");
}
}
public static void Main (string[] args) throws Exception
{
Qipan GB = new Qipan ();
Gb.initboard ();
Gb.printboard ();
This is the method for getting keyboard input
BufferedReader br = new BufferedReader (new InputStreamReader (system.in));
String inputstr = null;
System.out.println ("Please enter the coordinates of your chess, should be in X, y format:");
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)
{
Separates the user-entered string into 2 strings with a comma (,) as a delimiter
string[] Posstrarr = Inputstr.split (",");
Converts 2 strings to the coordinates of a user's chess
int xPos = Integer.parseint (posstrarr[0]);
int yPos = Integer.parseint (posstrarr[1]);
Assign the corresponding array element as "".
GB.BOARD[XPOS-1][YPOS-1] = "";
/*
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.
3. After each play, you need to scan who wins.
*/
Gb.printboard ();
System.out.println ("Please enter the coordinates of your chess, should be in X, y format:");
}
}
}

2.

Three. Write a program to convert an integer to a kanji reading string. For example, "1123" is converted to "1123"

1. SOURCE program:

Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;

public class Zhuanhuan {


public static void Main (string[] args) throws IOException {
TODO auto-generated Method Stub

Char b[]={' 0 ', ' one ', ' two ', ' three ', ' four ', ' five ', ' six ', ' seven ', ' eight ', ' nine '};
Char c[]={' ten ', ' hundred ', ' thousand ', ' million ', ' billion '};

System.out.println ("Please enter a number:");
BufferedReader number=new BufferedReader (New InputStreamReader (system.in));
String Num=number.readline ();
int Point=num.indexof (".");
Char[] A=num.tochararray ();

String output= "";

int n=a.length,j=0;

SYSTEM.OUT.PRINTLN (n);
for (int i=0;i<n;i++) {
J=n-i;
int d=a[i]-' 0 ';
if (j==1)
OUTPUT+=B[D];
else{

OUTPUT+=B[D];
OUTPUT+=C[J-2];
}

}

System.out.print (output);

}

}

2. Procedure:

Four. Can I change the amount represented in the figures to "Chinese character expression?" For example, the "¥123.52" to "one Bai San Wu three points"

1. Source Code

Import java.util.*;
public class Renminbi {

/**
* This function converts a floating-point number into renminbi-reading string
* 1 0000 0000
* Billions of dollars
* @param args
* @author Winds 185843144
*/
Defines a string array
Private string[] hanarr={"0", "one", "II", "three", "Restaurant", "WU", "Lu", "Qi", "ba", "JIU"};
Private string[] unitarr={"Pick", "Bai", "Thousand"};
Private string[] unitarr1={"Yuan", "million", "billion"};
Private string[] Divide (double num) {
Long Zheng = (long) num;//integral part

Long Xiao = (int) ((Num-zheng) *100);//Decimal part
if ((int) ((Num-zheng) *100) >=0&& (int) ((Num-zheng) *100) <10) {
return new String[]{zheng + "", "0" +string.valueof (xiao)};//convert integers to Strings
}else{
return new String[]{zheng + "", String.valueof (Xiao)};//convert integers to Strings
}

}

private string Tohanstr (string numstr) {
String result= "";//Results
int numlen = Numstr.length ();//The length of the numeric string

Four-digit start
if (numlen<=4) {
for (int i = 0;i<numlen;i++) {
int num = Numstr.charat (i) -48;//the char number minus 48 to get int type number
if (num!=0) {
if (i!=numlen-1) {
result+=hanarr[num]+unitarr[numlen-2-i];//if not the last digit, plus the unit "pick", "Bai", "thousand"
}else{
Result+=hanarr[num];
}
}else{
if (i<numlen-1&& (Numstr.charat (i+1) -48!=0)) {
RESULT+=HANARR[0];
}
}
}
if (result!= "") {
RESULT+=UNITARR1[0];
}
}else{//end of four digits
8-digit Start
if (numlen<=8) {
String qianw= "";//defines an tens of thousands of strings to obtain the results of the first four bits
String endf= "";//define a thousands of strings to get the result of the last four bits
for (int i = 0;i<numlen-4;i++) {//take top four bits
int num = Numstr.charat (i) -48;//the char number minus 48 to get int type number
if (num!=0) {
if (i!=numlen-5) {
qianw+=hanarr[num]+unitarr[numlen-6-i];//if not the last digit, plus the unit "pick", "Bai", "thousand"
}else{
Qianw+=hanarr[num];
}
}else{
if (i<numlen-1&& (Numstr.charat (i+1) -48!=0)) {
QIANW+=HANARR[0];
}
}
}
if (qianw!= "") {
QIANW+=UNITARR1[1];
}
RESULT+=QIANW;

Four digits after taking the number
for (int j=numlen-4;j<numlen;j++) {
int num = Numstr.charat (j) -48;//the char number minus 48 to get the int type number
if (num!=0) {
if (j!=numlen-1) {
endf+=hanarr[num]+unitarr[numlen-2-j];//if not the last digit, plus the unit "pick", "Bai", "thousand"
}else{
Endf+=hanarr[num];
}
}else{
if (j<numlen-1&& (Numstr.charat (j+1) -48!=0)) {
ENDF+=HANARR[0];
}
}
}
if (endf!= "") {
ENDF+=UNITARR1[0];
}
RESULT+=ENDF;
}else{
8-bit End

12-digit Start
if (numlen<=12) {
String yiw= "";//define an order to get the results of the first four bits of the million bits
String qianw= "";//defines an tens of thousands of strings to obtain the results of the first four bits
String endf= "";//define a thousands of strings to get the result of the last four bits
for (int k = 0;k<numlen-8;k++) {//take top four bits
int num = Numstr.charat (k) -48;//the char number minus 48 to get the int type number
if (num!=0) {
if (k!=numlen-9) {
yiw+=hanarr[num]+unitarr[numlen-10-k];//if not the last digit, plus the unit "pick", "Bai", "thousand"
}else{
Yiw+=hanarr[num];
}
}else{
if (k<numlen-9&& (Numstr.charat (k+1) -48!=0)) {
YIW+=HANARR[0];
}
}
}//top four-bit end
if (yiw!= "") {
YIW+=UNITARR1[2];
}
RESULT+=YIW;
for (int i = numlen-8;i<numlen-4;i++) {//take middle four bits
int num = Numstr.charat (i) -48;//the char number minus 48 to get int type number
if (num!=0) {
if (i!=numlen-5) {
qianw+=hanarr[num]+unitarr[numlen-6-i];//if not the last digit, plus the unit "pick", "Bai", "thousand"
}else{
Qianw+=hanarr[num];
}
}else{
if (i<numlen-5&& (Numstr.charat (i+1) -48!=0)) {
QIANW+=HANARR[0];
}
}
}//Middle four-bit end
if (qianw!= "") {
QIANW+=UNITARR1[1];
}
RESULT+=QIANW;

Four digits after taking the number
for (int j=numlen-4;j<numlen;j++) {
int num = Numstr.charat (j) -48;//the char number minus 48 to get the int type number
if (num!=0) {
if (j!=numlen-1) {
endf+=hanarr[num]+unitarr[numlen-2-j];//if not the last digit, plus the unit "pick", "Bai", "thousand"
}else{
Endf+=hanarr[num];
}
}else{
if (j<numlen-1&& (Numstr.charat (j+1) -48!=0)) {
ENDF+=HANARR[0];
}
}
}
if (endf!= "") {
ENDF+=UNITARR1[0];
}
RESULT+=ENDF;
}ELSE{//12 bit End
SYSTEM.OUT.PRINTLN ("This procedure can only convert thousand billion bits (including) the number below");

}
}
}


return results
return result;

}
Number of decimal parts
private string Tohanstrxiao (string numstr) {
String resultxiao= "";
if (Numstr.charat (0) -48==0) {
if (Numstr.charat (1) -48==0) {
Resultxiao= "";
}else{
resultxiao=hanarr[(Numstr.charat (1)-48)]+ "Min";
}

}else{
if (Numstr.charat (1) -48!=0) {
resultxiao=hanarr[(Numstr.charat (0)-48)]+ "Angle" +hanarr[(Numstr.charat (1)-48)]+ "Min";
}else{
resultxiao=hanarr[(Numstr.charat (0)-48)]+ "angle";
}

}
return Resultxiao;
}
public static void Main (string[] args) {
System.out.print ("Please enter Amount:");
Scanner input = new Scanner (system.in);
Double numdouble=input.nextdouble ();
Renminbi nr = new renminbi ();
string[] num = nr.divide (numdouble);
String result=nr.tohanstr (num[0]) +nr.tohanstrxiao (num[1]);
SYSTEM.OUT.PRINTLN (result);

}

}

2. Operation Result:

Five.

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

Requirements:

(1) Add and subtract two functions with your large number of classes

(2) Read the BigInteger class source code, figuring out what algorithm it uses to achieve subtraction four kinds of operations?

(3) through the Internet to find the large number of relevant data, to your large number of classes to add multiply, divide, and other functions to seek factorial.

Below to see what are the key attributes of BigInteger, the main ones are the following three:
(1) Final int signum
Signum attribute is to distinguish between positive and negative numbers and 0 of the flag bits, the JDK notes have been said to be clear:
the Signum of this BigInteger:-1 for negative, 0 for zero, or 1 for positive. Note that the BigInteger zero must has a signum of 0. This was necessary to ensures that there is exactly one representation for each BigInteger value.
(2) Final int[] Mag
Mag is an abbreviated form of magnitude, the mag array is stored BigInteger numerical size, in the order of Big-endian, that is, the high-level bytes into the low address, low-bit bytes into the high address, In a sequential manner. The original JDK notes are as follows:
The magnitude of this BigInteger, in Big-endian order:the zeroth element of this array is the most-signific Ant int of the magnitude. The magnitude must is "minimal" in the and the Most-significant Int (mag[0]) must be non-zero. This was necessary to ensure that there is exactly one representation for each BigInteger value. Note that this implies, the BigInteger Zero has a zero-length mag array.
(3) Final static long long_mask = 0xffffffffL;
This mask was used to obtain the value of an int as if it were unsigned.

Java 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.