標籤:bre 資料 加法 ext 輸出 輸入 byte init 關注
1.數組棋盤
import java.io.*;
public class QiPan
{
//定義一個二維數組來充當棋盤
private String[][] board;
//定義棋盤的大小
private static int BOARD_SIZE = 15;
public void initBoard()
{
//初始化棋盤數組
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的格式:");
}
}
}
2.編寫一個程式將整數轉化為漢字
import java.io.*;
public class Test1 {
public static void main(String[] args) {
//資料字典!
char c[]={‘零‘,‘壹‘,‘貳‘,‘三‘,‘肆‘,‘伍‘,‘陸‘,‘柒‘,‘捌‘,‘玖‘};
//等待輸入!
System.out.print("請輸入一個阿拉伯數字: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//得到輸入!
try {
String input=br.readLine();
for(int count=0;count<input.length();count++){
//轉成數字
char temp=input.charAt(count);
switch (temp){
case ‘1‘:System.out.print(c[1]);break;case ‘2‘:System.out.print(c[2]);break;
case ‘3‘:System.out.print(c[3]);break;case ‘4‘:System.out.print(c[4]);break;
case ‘5‘:System.out.print(c[5]);break;case ‘6‘:System.out.print(c[6]);break;
case ‘9‘:System.out.print(c[9]);break;case ‘8‘:System.out.print(c[8]);break;
case ‘0‘:System.out.print(c[0]);break;default:break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3大數
前面幾講介紹過JDK所提供的BigInteger能完成大數計算,如果不用它,直接使用數組表達大數,你能實現相同的功能嗎? 要求: (1)用你的大數類實現加和減兩個功能 (2)閱讀BigInteger類源碼,弄清楚它是使用什麼演算法實現加減乘除四種運算的? (3)通過互連網尋找大數運算的相關資料,給你的大數類添加乘、除、求階乘等其它功能。
(1)BigInteger曆史介紹
在java中,存在很多種類的資料類型,例如byte short char int float double long,而BigInteger屬於其中一個比較特殊的資料類型,也是本教程關注的重點。BigInteger在JDK1.1中就已經存在了,屬於java.math包的類。從名字來看,BigInteger比Integer表示數值的範圍更大一些。BigInteger類的基本結構如下所示:
java.lang.Object
|_java.lang.Number
|_java.math.BigInteger
BigInteger已實現的介面:Serializable, Comparable<BigInteger>
(2)BigInteger是不可變的任意精度的整數。所有操作中,都以二進位補碼形式表示 BigInteger(如 Java 的基本整數類型)。BigInteger 提供所有 Java 的基本整數操作符的對應物,並提供 java.lang.Math 的所有相關方法。另外,BigInteger 還提供以下運算:模算術、GCD 計算、質數測試、素數產生、位操作以及一些其他動作。
(3)BigInteger屬性分析
下面看看BigInteger有哪些重點的屬性,主要的有下面三個:
(1)final int signum
signum屬性是為了區分:正負數和0的標誌位,JDK注釋裡面已經說的很明白了:
The signum of this BigInteger: -1 for negative, 0 for zero, or 1 for positive. Note that the BigInteger zero must have a signum of 0. This is necessary to ensures that there is exactly one representation for each BigInteger value.
(2)final int[] mag
mag是magnitude的縮寫形式,mag數組是儲存BigInteger數值大小的,採用big-endian的順序,也就是高位位元組存入低地址,低位位元組存入高地址,依次排列的方式。JDK原文注釋如下:
The magnitude of this BigInteger, in big-endian order: the zeroth element of this array is the most-significant int of the magnitude. The magnitude must be "minimal" in that the most-significant int (mag[0]) must be non-zero. This is necessary to ensure that there is exactly one representation for each BigInteger value. Note that this implies that the BigInteger zero has a zero-length mag array.
(3)final static long LONG_MASK = 0xffffffffL;
This mask is used to obtain the value of an int as if it were unsigned。
import java.util.Scanner;public class BigNum { public static void main(String[] args) { // TODO Auto-generated method stub int aa,bb; System.out.println("用數組實現大數的加法和減法"); System.out.print("請輸入大數a:"); Scanner scan=new Scanner(System.in); String a=scan.next(); System.out.print("請輸入大數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); }}
4.隨機數
import javax.swing.*;
public class Sum {
public static void main(String args[])
{
String output= "10個1000以內的隨機數為:\n";
int sum=0;
int a []=new int [10];
for(int i = 0;i<10;i++)
{
a[i]=(int) (Math.random()*1000);
output += " "+a[i];
sum += a[i];
}
output +="\n\n十個數的和是:"+sum;
JOptionPane.showMessageDialog(null,output,"結果",
JOptionPane.PLAIN_MESSAGE);
}
}
開始-》int=0 sum=0-》i<10如果大於10就輸出總合-》a[i]=Math。random()*1000-》sum+=a[i]-》
輸出a[i]-》輸出sum-》結束 。。
java數組課堂