1. Read the Qipan.java sample program to learn how to draw a Gobang disk using two-dimensional arrays and circular statements.
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:");
}
}
}
Experimental results:
2. Please write a program to convert an integer into a kanji reading method.
Experimental ideas:
1, the 0-9 of Chinese characters read the method to write to an array
2. Write the number of digits to another array
3, the user input a number, as a string input, and then converted to an array of objects
4, with the J mark the first few, the character into a number, not the second and the corresponding number of digits to read the method
5. The output is concatenated with a string
SOURCE program:
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
public class Hanzidufa {
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);
}
}
Results:
3 Change the amount represented by the number to the Chinese character expression
Ideas:
1, two arrays, one is the uppercase number, the other is the number of digits
2. Enter the amount and convert to an array
3. Find the location of the decimal point
4, read before the decimal point, read from the first to the decimal point, up to two digits after the decimal point
Source
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStreamReader;
public class Jiner {
public static void Main (string[] args) throws IOException {
TODO auto-generated Method Stub
Char b[]={' 0 ', ' one ', ' II ', ' three ', ' establishments ', ' Wu ', ' Lu ', ' qi ', ' ba ', ' JIU '};
Char c[]={' pick ', ' bai ', ' thousand ', ' million ', ' billion '};
System.out.println ("Please enter Amount:");
BufferedReader number=new BufferedReader (New InputStreamReader (system.in));
String Num=number.readline ();
int Point=num.indexof ("."); /subscript
Char[] A=num.tochararray ();
String output= "";
int n=a.length,j=0;
for (int i=0;i<point;i++) {
J=point-i;
int d=a[i]-' 0 ';
if (j==1)
OUTPUT+=B[D];
else{
OUTPUT+=B[D];
OUTPUT+=C[J-2];
}
}
output+= "Yuan";
if (a.length>point) {
int d=a[point+1]-' 0 ';
output+=b[d]+ "angle";
if ((point+3) ==a.length)
{
int e=a[point+2]-' 0 ';
output+=b[e]+ "points";
}
}
System.out.print (output);
}
}
Program results:
4. Randomly generate 10 numbers, populate an array, then output the array contents with a message box, then compute the array elements and display the results in a message box.
Program Design Ideas:
1. Create an integer array, define the string output, define the variable b=0
2. Generate random numbers with math.random ()
3, dialog box output these 10 random numbers
3. Add each element
4, with the dialog box output and
Flow chart:
Source:
Import Java.util.Random;
Import Javax.swing.JOptionPane;
public class Suijishuzu {
public static void Main (string[] args) {
TODO auto-generated Method Stub
String output= "";
int a[]=new int[10];
int b=0;
output+= "10 random numbers generated are: \ n";
for (int i=0;i<a.length;i++) {
a[i]= (int) (Math.random () *1000);
Output+=a[i]+ "";
}
Joptionpane.showmessagedialog (null, output, "information", joptionpane.information_message);
for (int i=0;i<a.length;i++) {
B+=a[i];
}
output+= "\ n" The result of each number added is: "+B;
Joptionpane.showmessagedialog (Null,output);
}
}
5, the previous talk about 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.
Package Work;import Java.util.scanner;public class Bignum {public static void Main (string[] args) { //TODO auto- Generated method stub int aa,bb; System.out.println ("Adding and subtracting large numbers with arrays"); System.out.print ("Please enter a large number A:"); Scanner scan=new Scanner (system.in); String A=scan.next (); System.out.print ("Please enter a large number B:"); String B=scan.next (); int a[]=new int[100]; int b[]=new int[100]; for (int i=0;i<a.length (); i++) { a[i]= (int) ((A.charat (i) -48) *math.pow (10,a.length ()-i-1)); } for (int i=0;i<b.length (); i++) { b[i]= (int) ((B.charat (i) -48) *math.pow (10,b.length ()-i-1)); } int sum=0; int sub=0; for (int i=0;i<a.length (); i++) { sum+=a[i]+b[i]; Sub+=a[i]-b[i]; } System.out.print ("a+b=" +sum); System.out.println (); System.out.print ("a-b=" +sub);}
② Running results:
(2) read the BigInteger class source code, figuring out what algorithm it uses to achieve subtraction four kinds of operations?
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.
(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.
① Source code:
Adding and subtracting large numbers with arrays
Package Work;import Java.util.scanner;public class Bignum {public static void Main (string[] args) { //TODO auto- Generated method stub int aa,bb; System.out.println ("Adding and subtracting large numbers with arrays"); System.out.print ("Please enter a large number A:"); Scanner scan=new Scanner (system.in); String A=scan.next (); System.out.print ("Please enter a large number B:"); String B=scan.next (); int a[]=new int[100]; int b[]=new int[100]; for (int i=0;i<a.length (); i++) { a[i]= (int) ((A.charat (i) -48) *math.pow (10,a.length ()-i-1)); } for (int i=0;i<b.length (); i++) { b[i]= (int) ((B.charat (i) -48) *math.pow (10,b.length ()-i-1)); } int sum=0; int sub=0; for (int i=0;i<a.length (); i++) { sum+=a[i]+b[i]; Sub+=a[i]-b[i]; } System.out.print ("a+b=" +sum); System.out.println (); System.out.print ("a-b=" +sub);}
② Running results:
(2) read the BigInteger class source code, figuring out what algorithm it uses to achieve subtraction four kinds of operations?
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.
(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.
① Source code:
Adding and subtracting large numbers with arrays
Package Work;import Java.util.scanner;import Java.math.bigdecimal;public class Bignum {public static void main (string[ ] args) {//TODO auto-generated method stub int aa,bb; System.out.println ("Adding and subtracting large numbers with arrays"); System.out.print ("Please enter a large number A:"); Scanner scan=new Scanner (system.in); String A=scan.next (); System.out.print ("Please enter a large number B:"); String B=scan.next (); int a[]=new int[100]; int b[]=new int[100]; For complex multiplication and division BigDecimal b1 = new BigDecimal (a); BigDecimal b2 = new BigDecimal (b); for (int i=0;i<a.length (); i++) {a[i]= (int) ((A.charat (i) -48) *math.pow (10,a.length ()-i-1)); } for (int i=0;i<b.length (); i++) {b[i]= (int) ((B.charat (i) -48) *math.pow (10,b.length ()-i-1)); } int sum=0; int sub=0; for (int i=0;i<a.length (); i++) {sum+=a[i]+b[i]; Sub+=a[i]-b[i]; } System.out.print ("a+b=" +sum); System.out.println (); System.out.print ("a-b=" +sub); SysteM.out.println (); System.out.print ("a*b=" +b1.multiply (B2)); System.out.println (); System.out.print ("a/b=" +b1.dividetointegralvalue (B2)); } }
② Running results:
Package Work;import Java.util.scanner;import Java.math.bigdecimal;public class Bignum {public static void main (string[ ] args) {//TODO auto-generated method stub int aa,bb; System.out.println ("Adding and subtracting large numbers with arrays"); System.out.print ("Please enter a large number A:"); Scanner scan=new Scanner (system.in); String A=scan.next (); System.out.print ("Please enter a large number B:"); String B=scan.next (); int a[]=new int[100]; int b[]=new int[100]; For complex multiplication and division BigDecimal b1 = new BigDecimal (a); BigDecimal b2 = new BigDecimal (b); for (int i=0;i<a.length (); i++) {a[i]= (int) ((A.charat (i) -48) *math.pow (10,a.length ()-i-1)); } for (int i=0;i<b.length (); i++) {b[i]= (int) ((B.charat (i) -48) *math.pow (10,b.length ()-i-1)); } int sum=0; int sub=0; for (int i=0;i<a.length (); i++) {sum+=a[i]+b[i]; Sub+=a[i]-b[i]; } System.out.print ("a+b=" +sum); System.out.println (); System.out.print ("a-b=" +sub); SysteM.out.println (); System.out.print ("a*b=" +b1.multiply (B2)); System.out.println (); System.out.print ("a/b=" +b1.dividetointegralvalue (B2)); } }
② Running results:
Java Job 06