一、瞭解棋盤打印:利用二維數組輸出一個15*15的棋盤,棋盤的原素爲「+」,就是輸出一個橫縱都是15個「+」的二維數組,而後再以座標形式輸入棋子「●」,替換掉原來棋盤裏的「+」。再編寫代碼。 電腦隨機生成2個整數,做爲電腦下棋的座標,賦給board數組。還涉及1.座標的有效性,只能是數字,不能超出棋盤範圍2.若是下的棋的點,不能重複下棋。3.每次下棋後,須要掃描誰贏了java
代碼以下:算法
import java.io.*;數組
public class QiPandom
{ide
//定義一個二維數組來充當棋盤函數
private String[][] board;this
//定義棋盤的大小spa
private static int BOARD_SIZE = 15;設計
public void initBoard()orm
{
//初始化棋盤數組
board = new String[BOARD_SIZE][BOARD_SIZE];
//把每一個元素賦爲"╋",用於在控制檯畫出棋盤
for (int i = 0 ; i < BOARD_SIZE ; i++)
{
for ( int j = 0 ; j < BOARD_SIZE ; j++)
{
board[i][j] = "╋";
}
}
}
//在控制檯輸出棋盤的方法
public void printBoard()
{
//打印每一個數組元素
for (int i = 0 ; i < BOARD_SIZE ; i++)
{
for ( int j = 0 ; j < BOARD_SIZE ; j++)
{
//打印數組元素後不換行
System.out.print(board[i][j]);
}
//每打印完一行數組元素後輸出一個換行符
System.out.print("\n");
}
}
public static void main(String[] args)throws Exception
{
QiPan gb = new QiPan();
gb.initBoard();
gb.printBoard();
//這是用於獲取鍵盤輸入的方法
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputStr = null;
System.out.println("請輸入您下棋的座標,應以x,y的格式:");
//br.readLine():每當在鍵盤上輸入一行內容按回車,剛輸入的內容將被br讀取到。
while ((inputStr = br.readLine()) != null)
{
//將用戶輸入的字符串以逗號(,)做爲分隔符,分隔成2個字符串
String[] posStrArr = inputStr.split(" ");
//將2個字符串轉換成用戶下棋的座標
int xPos = Integer.parseInt(posStrArr[0]);
int yPos = Integer.parseInt(posStrArr[1]);
//把對應的數組元素賦爲"●"。
gb.board[xPos - 1][yPos - 1] = "●";
/*
電腦隨機生成2個整數,做爲電腦下棋的座標,賦給board數組。
還涉及
1.座標的有效性,只能是數字,不能超出棋盤範圍
2.若是下的棋的點,不能重複下棋。
3.每次下棋後,須要掃描誰贏了
*/
gb.printBoard();
System.out.println("請輸入您下棋的座標,應以x,y的格式:");
}
}
}
二、數字轉換:編寫一個程序將一個整數轉換爲漢字讀法字符串。好比「1123」轉換爲「一千一百二十三」
代碼:import java.util.Scanner;
public class ChangeRMB {
public static void main(String[] args) {
ChangeRMB t2r = new ChangeRMB();
String s = t2r.cleanZero(t2r.splitNum(t2r.roundString(t2r.getNum())));
// 若是轉換事後是一個空串,則不輸出屏幕
if(!"".equals(s)) {
System.out.println("轉換成中文後爲:" + s);
}
}
private String getNum() {
String s = null;
System.out.println("請輸入一個數字(非負數,精確到小數點後兩位):");
Scanner scanner = new Scanner(System.in);
s = scanner.next();
scanner.close();
return s;
}
numFormat() 方法
private String splitNum(String s) {
if("".equals(s)) {
return "";
}
int index = s.indexOf(".");
String intOnly = s.substring(0, index);
String part1 = this.numFormat(1, intOnly);
String smallOnly = s.substring(index + 1);
String part2 = this.numFormat(2, smallOnly);
String newS = part1 + part2;
return newS;
}
private String roundString(String s) {
if("".equals(s)) {
return "";
}
int index = s.indexOf(".");
String intOnly = s.substring(0, index);
if(intOnly.length() > 13) {
System.out.println("輸入數據過大!(整數部分最多13位!)");
return "";
}
String smallOnly = s.substring(index + 1);
if(smallOnly.length() > 2) {
String roundSmall = smallOnly.substring(0, 2);
s = intOnly + "." + roundSmall;
}
return s;
}
private String numFormat(int flag, String s) {
int sLength = s.length();
String bigLetter[] = {"零", "壹", "貳", "叄", "肆", "伍", "陸", "柒", "捌", "玖"};
String unit[] = {"元", "拾", "佰", "仟", "萬", "拾", "佰", "仟", "億", "拾", "佰", "仟", "萬"};
String small[] = {"分", "角"};
String newS = "";
for(int i = 0; i < sLength; i ++) {
if(flag == 1) {
// 轉換整數部分爲中文大寫形式(帶單位)
newS = newS + bigLetter[s.charAt(i) - 48] + unit[sLength - i - 1];
}
else if(flag == 2) {
newS = newS + bigLetter[s.charAt(i) - 48] + small[sLength - i - 1];
}
}
return newS;
}
private String cleanZero(String s) {
if("".equals(s)) {
return "";
}
慣
while(s.charAt(0) == '零') {
s = s.substring(2);
if(s.length() == 0) {
return "零圓";
}
}
String regex1[] = {"零仟", "零佰", "零拾"};
String regex2[] = {"零億", "零萬", "零元"};
String regex3[] = {"億", "萬", "元"};
String regex4[] = {"零角", "零分"};
for(int i = 0; i < 3; i ++) {
s = s.replaceAll(regex1[i], "零");
}
for(int i = 0; i < 3; i ++) {
s = s.replaceAll("零零零", "零");
s = s.replaceAll("零零", "零");
s = s.replaceAll(regex2[i], regex3[i]);
}
for(int i = 0; i < 2; i ++) {
s = s.replaceAll(regex4[i], "");
}
s = s.replaceAll("億萬", "億");
return s;
}
}
3、大數
(1)JDK所提供的BigInteger
(2)閱讀BigInteger類源碼,弄清楚它是使用什麼算法實現加減乘除四種運算的?
(3)經過互聯網查找大數運算的相關資料,給你的大數類添加乘、除、求階乘等其它功能。
代碼:package biginteger;
import java.math.BigInteger;
import java.util.Scanner;
public class Biginteger {
public static void main(String[] args) {
// TODO 自動生成的方法存根
System.out.print("請輸入3個數");
Scanner input=new Scanner(System.in);
int x=input.nextInt();
BigInteger a=BigInteger.valueOf(x);
/*int y=input.nextInt();
BigInteger b=BigInteger.valueOf(y); */
BigInteger z=input.nextBigInteger();
BigInteger h=input.nextBigInteger();
System.out.print("大數的和爲"+h.add(z));//大數相加
System.out.print("\n大數的差爲"+h.subtract(z));//大數相減
System.out.print("\n大數的乘積爲"+h.multiply(z));//大數相乘
System.out.print("\n大數相除取整爲"+h.divide(z));//大數相除
System.out.print("\n大數a的絕對值爲"+h.abs());//絕對值
System.out.print("\n大數的最大公約數爲"+h.gcd(z));//最大公約數
System.out.print("\n大數a的相反數爲"+h.negate());//取相反數
System.out.print("\n大數中較大的爲"+h.max(z));//比大小(a.min(b))
System.out.print("\n大數是否相等"+h.equals(z));//比相等
System.out.println("\n"+a+"!="+calculateN2(a));//階乘
}
public static BigInteger calculateN2(BigInteger n) {
if(n.equals(1) || n.equals(0)){
return BigInteger.valueOf(1);
}
return n.multiply(calculateN2(n.subtract(new BigInteger("1"))));
}
}
截圖:
四.數組隨機數
1.設計思路:編寫代碼產生10個隨機數,用數組記錄,分別用消息框輸出數組的下標和每一個下標對應的數。經過數組計算這10個隨機數的和並用消息框輸出。
2程序設計流程圖
代碼:package random;
import java.math.BigInteger;
import javax.swing.*;
public class Random {
public static void main(String[] args) {
// TODO 自動生成的方法存根
BigInteger m=new BigInteger("100");
int c=0;
int a[]=new int [10];
System.out.print("數組的各元素爲");
for(int i=0;i<10;i++)
{
a[i]=(int)( Math.random()*100);
c+=a[i];
System.out.print(a[i]+" ");
}
BigInteger b=BigInteger.valueOf(c);
String output = "";
output += "Subscript\tValue\n";
for ( int i = 0; i < a.length; i++ )
output += i + "\t" + a[ i ] + "\n";
JTextArea outputArea = new JTextArea( 11, 10 );
outputArea.setText( output );
JOptionPane.showMessageDialog( null, outputArea,
"Initializing an Array with a Declaration",
JOptionPane.INFORMATION_MESSAGE );
JOptionPane.showMessageDialog( null, b,
"數組元素的和爲",JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}//時間函數System.currenttTimeMillis(),System.currentTimeMillis()
}
截圖:
錯誤分析:這個程序相比來講是簡單的了,在看看老師的課件,沒什麼問題。