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.math.BigInteger;
public class BigInt {
public static void main(String[] args) {
BigInteger aa =new BigInteger("10");
BigInteger bb= new BigInteger("10");
BigInteger sub=aa.subtract(bb);//大整数的减
BigInteger add=aa.add(bb);//大整数的加
BigInteger mul=aa.multiply(bb);//大整数的乘
BigInteger div=aa.divide(bb);//大整数的除
System.out.println("大整数的减:"+sub.toString());
System.out.println("大整数的加:"+add.toString());
System.out.println("大整数的乘:"+mul.toString());
System.out.println("大整数的除:"+div.toString());
}
}
大数的相关知识:
前面几讲介绍过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。
3.字符转换
import javax.swing.*;
public class StrChange {
private static String [] hanarr={"壹","贰","叁","肆","伍","陆","柒"
,"捌","镹"};
private static String []unit={"分","角","元","拾","佰","仟","万","拾",
"佰","仟","亿","十","佰","仟"};
private static String toHan(String number){
String result="";
int numlen=number.length();
for(int i=0;i<numlen-3;i++){
int num=number.charAt(i)-‘0‘;
if(num!=0){
result+=hanarr[num-1]+unit[number.length()-2-i];
}
else if(number.charAt(i-1)-‘0‘==0){
continue;
}
else if(num==0){
result+="零";
}
}
for( int i=numlen-2;i<numlen;i++){
int num=number.charAt(i)-‘0‘;
if(num!=0){
result+=hanarr[num-1]+unit[number.length()-1-i];
}
else if(number.charAt(i-2)-‘0‘==0||number.charAt(i-1)-‘0‘==0){continue;}
else {
result+="零";
}
}
return result;
}
public static void main(String args []){
String a=JOptionPane.showInputDialog("请输入一个带小数点数字");
JOptionPane.showMessageDialog(null,toHan(a),"结果转换为汉字", JOptionPane.PLAIN_MESSAGE);
}
}
4.随机数
import javax.swing.*;
public class Random {
public static void main(String args[]){
int a[] = new int[10];
String str = "";
int sum = 0;
for(int i = 0;i < a.length;i++){
a[i] = (int)(Math.random()*20);
str += a[i];
sum += a[i];
}
for(int i =0;i < a.length;i++)
{
JOptionPane.showMessageDialog(null, "第"+(i+1)+"个数为:"+a[i],null,JOptionPane.PLAIN_MESSAGE);
}
JOptionPane.showMessageDialog(null, "这十个数的总和为::"+sum,null,JOptionPane.PLAIN_MESSAGE);
}
}
程序流程图:
设计思路:随机生成十个数,然后设定一个sum变量作为求和变量,用for循环在对话框中输出
结果截图: