思考:当需要用户输入一个整数时,用户输入了一个字符串,如何处理类似问题呢?
C++
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please Input a int num:");
//检验Scanner存储的数据是时一个整数,如果Scanner中没有数据则先让用户输入一个数据。
if(sc.hasNextInt()){//sc.hasNextDouble()
int number = sc.nextInt();//讲Scanner中存储的数据读取出来
System.out.println(number);
}
else{
System.out.println{"请输入一个整数"};
}
}
}
1.语法规则
PLAINTEXT
标号名称: 循环结构
2.作用
标号的作用就是给代码添加一个标记,方便后面使用。通常应用在循环结构中,与break语句配合使用
3.应用场景
实现其中返回主菜单的功能
JAVA
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(true){
System.out.println("=========================");
System.out.println("1.学生成绩管理");
System.out.println("2.学生选课管理");
System.out.println("3.退出系统");
System.out.println("=========================");
System.out.println("请选择菜单编号:");
int menuNo = sc.nextInt();
if(menuNo == 1){
childMenu:while(true){
System.out.println("**************************");
System.out.println("1.添加成绩");
System.out.println("2.查看成绩");
System.out.println("3.修改成绩");
System.out.println("4.删除成绩");
System.out.println("5.返回主菜单");
System.out.println("**************************");
System.out.println("请选择菜单编号:");
int number = sc.nextInt();
switch (number){
case 1:
System.out.println("你选择添加成绩");
break;
case 2:
System.out.println("你选择查看成绩");
break;
case 3:
System.out.println("你选择修改成绩");
break;
case 4:
System.out.println("你选择删除成绩");
break;
case 5:
System.out.println("你选择返回主菜单");
break childMenu; //java中的标号,可以理解为一个代码的标记
}
}
} else if(menuNo == 2){
} else {
System.out.println("感谢使用本人开发的系统");
break; //终止break所在的循环
}
}
}
数组的定义
PLAINTEXT
数据类型[] 数组名;
数组名 = new 数据类型[数组的长度];
数组名 = new 数据类型[]{元素1, 元素2, ..., 元素n};
数据类型[] 数组名 = new 数据类型[数组的长度];
数据类型[] 数组名 = {元素1, 元素2, ..., 元素n};
内存模型
JAVA
public class Main{
public static void main(String[] args){
//回顾变量的定义: 数据类型 变量名;
//1.数组的定义: 数据类型[] 数组名;
String name; //存储一个名字
String[] names; //存储很多名字
//回顾变量的赋值: 变量名 = 变量的值;
//数组的赋值: 数组名 = new 数据类型[数组的长度];
name = "刘德华";
names = new String[10];
//回顾变量的定义: 数据类型 变量名 = 变量的值;
//2.数组的定义: 数据类型[] 数组名 = new 数据类型[数组的长度];
double score = 90.5;
double[] scores = new double[5];
//3.数组的定义: 数据类型[] 数组名 = {元素1,元素2, ....};
int[] numbers = {1, 2, 3, 4, 5};
//4.数组的定义: 数据类型[] 数组名 = new 数据类型[]{元素1,元素2, ....};
byte[] bytes = new byte[]{1,2,3,4,5};
}
}
上面的代码中有定义数组:double[] scores = new double[5];思考数组中存储的元素是什么?
双精度浮点数数组中的默认值为0.0,单精度浮点数数组中的默认值为0.0f。boolean类型数组默认元 素为false。char类型数组中的默认元素为’\u0000’,整形数组默认元素为0
思考 char[] sexArr = {'M', 'F', 'O'};
和 char[] sexArr = new char[]{'M', 'F', 'O'};
有什么区别?
第一种方式只能在定义数组同时赋值时使用,第二种方式可以在定义数组时直接使用,也可以先定义数 组,然后再赋值时使用
4. 数组的基本要素
标识符
也就是数组的名称,只是在数组中的一个专业术语,本质就是一个变量名
数组元素
也就是数组中的每一块空间存储的数据
元素类型
也就是数组存放的数据类型
元素下标
数组中每一个元素所处的位置就是数组元素的下标,数组元素下标从0开始,因此,数组元素下标的最
大值为数数组长度 - 1
5. 数组的特性
数组的长度一旦定义就不能发生改变,除非给数组重新赋值,才能改变数组的大小
1.数组的遍历
JAVA
public class Main{
public static void main(String[] args){
int[] num = {1,2,3,4,5,6};
//数组的长度: 数组名.length 其中 '.'读作 的
for(int i = 0;i < num.length; i++){
System.out.println(num[i]);
}
}
}
2.向数组中添加元素
案例
在某机票代售点有A、B、C、D、E 5人正排队购票,B的好朋友F现在也来排队购票,发现B正在排队,
于是插队至B的后面,请使用数组的相关知识完成程序设计。
分析
a. 构建一个字符串数组存储ABCDE5人
b. F来插队,插队到B的后面,那么B后面的人所处位置相当于往后挪动一位
代码实现
JAVA
public class Main{
public static void main(String[] args){
// A B C D E
// A B F C D E
String[] personArr = {"A","B","C","D","E"};
String[] newArr = new String[personArr.length + 1]; //新建一个数组,长
度比原来的数组多1
int index = 2; //F的位置
for(int i=0; i<index; i++){
newArr[i] = personArr[i];//将personArr数组中的元素直接拷贝过来
}
newArr[index] = "F";
for(int i=index; i<personArr.length; i++){
newArr[i+1] = personArr[i];//将personArr数组中B后面的元素挪动过来,但位置需要加1
}
personArr = newArr;
for(int i=0; i<personArr.length;i++){
System.out.println(personArr[i]);
}
}
}
3.删除数组中的元素
案例
在前面的案例中,购票人C因为中途有事而离开,排队的人员减少了一个。请使用数组的相关知识完成
程序设计。
分析
a. 使用数组存储目前排队的人ABFCDE
b. C离开后,排队的人员减少了1个,C后面的人向前挪动一位
代码实现
JAVA
public class Example7 {
public static void main(String[] args) {
String[] personArr = {"A","B","F","C","D","E"};
//A B F C D E
//A B F D E
String[] newArr = new String[personArr.length - 1];
int index = 3; //C的位置
for(int i=0; i<index; i++){
newArr[i] = personArr[i];
}
for(int i=index+1; i<personArr.length; i++){
newArr[i-1] = personArr[i];
}
personArr = newArr;
for(int i=0; i<personArr.length; i++){
System.out.println(personArr[i]);
}
}
}
4.数组拷贝
语法
JAVA
System.arrayCopy(原数组, 拷贝的开始位置, 目标数组, 存放的开始位置, 拷贝的元素个数);
示例
JAVA
public class Main{
public static void main(String[] args){
//A B C D E
//A B F C D E
String[] personArr = {"A","B","C","D","E"};
String[] newArry = new String[personArr.length +1];
int index =2;
//数组拷贝
System.arraycopy(personArr, 0, newArr, 0, index);
newArr[index] = "F";
System.arraycopy(personArr, index, newArr, index+1,personArr.length - index);
personArr = newArr;
for(int i=0; i<personArr.length;i++){
System.out.println(personArr[i]);
}
}
}
JAVA
public class Example7 {
public static void main(String[] args) {
String[] personArr = {"A","B","F","C","D","E"};
//A B F C D E
//A B F D E
String[] newArr = new String[personArr.length - 1];
int index = 3; //C的位置
System.arraycopy(personArr, 0, newArr, 0, index);
System.arraycopy(personArr, index+1, newArr, index,personArr.length - index - 1);
personArr = newArr;
for(int i=0; i<personArr.length; i++){
System.out.println(personArr[i]);
}
}
}
5.数组扩容
语法
JAVA
数据类型[] 标识符 = Arrays.copyOf(原数组, 新数组的长度);
示例
JAVA
public class Example8 {
public static void main(String[] args) {
String[] personArr = {"A","B","C","D","E"};
//数组扩容: 第一个参数表示要扩容的数组 第二个参数表示,扩容后的新的数组的长度
//作用: 新建一个数组,并将原数组的所有元素全部拷贝至新数组中
//newArr = {"A","B","C","D","E", null}
String[] newArr = Arrays.copyOf(personArr, personArr.length + 1);
int index = 2;
// A B C D E null => A B C C D E
System.arraycopy(newArr, index, newArr, index+1, personArr.length -index);
//A B C C D E => A B F C D E
newArr[index] = "F";
personArr = newArr;
for(int i=0; i<personArr.length; i++){
System.out.println(personArr[i]);
}
}
}
JAVA
public class Main{
public static void main(String[] args){
int[] numbers = {10,70,55,80,25,26};
for(int i=0; i<numbers.length; i++){//控制遍历次数
for(int j=0; j<numbers.length - i - 1; j++){
if(numbers[j] < numbers[j+1]){
int temp = numbers[j]; //使用临时变量保存其中一个元素的值
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}
for(int i=0; i<numbers.length; i++){
System.out.println(numbers[i]);
}
}
}
工具类排序
语法:
PLAINTEXT
Arrays.sort(数组名); //将数组中的元素进行升序排列
Arrays.toString(数组名);//将数组中的元素组装为一个字符串
示例:
JAVA
public class Example2 {
public static void main(String[] args) {
//字符能够排序,排序是按照字典的顺序进行排序
char[] chars = {'c','a','g','p','f'};
Arrays.sort(chars);//对数组进行升序排列
System.out.println(Arrays.toString(chars));
String[] names = {"zhangsan", "zhangsi", "lisi","lisan", "lisiabc","lisib"};
Arrays.sort(names);
System.out.println(Arrays.toString(names));
}
}
解释说明
二分查找法又称为折半查找,顾名思义,每一次都会将数组从中间分为两个区间,利用中间元素与要查找的目标元素比较大小。从而确定目标元素所处的区间。依次往复执行,直到找到目标元素为止。
二分查找只适用于已经排好序的数组
案例
从数列95,93,87,86,79,72,60,53中快速找出元素60所处位置
分析
a. 首先将数组从中间位置一分为二,然后利用中间位置的元素与60进行比较,从而确定60所处的目标区间
b. 再将目标区间从中间位置一分为二,然后利用中间位置的元素与60进行比较,从而确定60所处的目标区间
c. 再将目标区间从中间位置一分为二,然后利用中间位置的元素与60进行比较,从而确定60所处的目标区间
代码实现
JAVA
public class Example3 {
public static void main(String[] args) {
int[] numbers = {95,93,87,86,79,72,60,53};
int target = 60; //要查找的目标元素
int start = 0;//区间取值的最小值
int end = numbers.length - 1;//区间取值的最大值
while (start < end){ //开始位置比结束位置小,就一直不停的循环
int middle = (start + end) / 2;
if(numbers[middle] > 60){//说明目标元素在后半区间
start = middle + 1; //改变区间开始的位置
} else if(numbers[middle] < 60){//说明目标元素在前半区间
end = middle - 1; //改变区间结束的位置
} else {
System.out.println("目标元素" + target + "所处位置:" + middle);
break;
}
}
}
}
数组的本质
数组从本质上来说只有一维,二维数组是指在一维数组中再放入一个一维数组。三维数组、四维数组依次类推。
二维数组的定义
语法
PLAINTEXT
数据类型[][] 数组名 = new 数据类型[数组的长度][数组的长度]; 1
示例
JAVA
public class Main{
public static void mian(String[] args){
String[][] musicInfos = new String[5][3];
Scanner sc = new Scanner(System.in);
for(int i=0; i<musicInfos.length;i++){
System.out.println("请输入歌曲名称:");
String name = sc.next();
System.out.println("请输入歌曲歌手:");
String singer = sc.next();
System.out.println("请输入歌曲出版年月:");
String date = sc.next();
musicInfos[i] = new String[]{name, singer, date};
}
}
}
案例
从控制台录入5首音乐信息(包括名称、歌手、出版年月),并将这些信息存储在数组中。
代码实现
JAVA
public class Main{
public static void main(STring[] args){
String[][] musicInfors = new String[5][3];
Scanner sc = new Scanner(System.in);
for(int i=0; i<musicInfos.length;i++){
System.out.println("请输入歌曲名称:");
String name = sc.next();
System.out.println("请输入歌曲歌手:");
String singer = sc.next();
System.out.println("请输入歌曲出版年月:");
String date = sc.next();
musicInfos[i] = new String[]{name, singer, date};
}
}
}
练习
从控制台录入10所高校信息(包括高校名称、所处位置、创办年月),并将这些信息存储在数组中。某学校一年级共有3个班,第一个班10人,第二个班8人,第三个班7人,现要求从控制台录入这3个班学生的成绩和年龄,并计算出每个班的平均成绩和平均年龄。
JAVA
public class Main{
public static void mian(String[] args){
double[][][] stuInfos = new double[3][][];
stuInfos[0] = new double[10][2];
stuInfor[1] = new double[8][2];
stuInfor[2] = new double[7][2];
Scanner sc = Scanner(System.in );
for(int i = 0;i<stuInfors.length;i++){
double[][] infors = stuInfors[i];
for(int j = 0;j < infors.length;j++){
System.out.println("请输入年龄");
int age = sc.nextInt();
System.out.println("请输入成绩");
double score = sc.nextDouble();
intfos[j] = new Double[]{age,score};
}
//写法一
for(ini i = 0; i<stuInfos.length;i++){
double totalAge = 0,totalScore = 0;
for(int j = 0;j < stuInfos[i].length;j++){
totalAge += stuInfors[i][j][0];
}
}
//写法二
for(ini i = 0; i<stuInfos.length;i++){
double totalAge = 0,totalScore = 0;
double[][] infors = stuInfors[i];
for(int j = 0;j < infos.length;j++){
totalAge += stuInfors[j][0];
}
}
System.out.println("第" + (i+1) + "个班的平均成绩为:" + totalAge / infors.length);
}
}
}
项目需求
五子棋棋盘为一个10 x 10的棋盘,五子棋玩家共2个(这里分别称为A和B),A在棋盘上落子后,B再落子,依次往复,直到一方胜利或者棋盘空间用完为止。判断胜利的条件是一条直线或者任意斜线上同时存在A或者B的连续5颗棋子(见下图)。
技术要求:
1.静态变量
语法
PLAINTEXT
public static 数据类型 变量名 = 变量名;
解释说明
静态变量只能定义类中,不能定义在方法中。静态变量可以在static修饰的方法中使用,也可以在非静
态的方法中访问。主要解决在静态方法中不能访问非静态的变量。
示例
JAVA
public static void main(String[] args) {
show();
show();
show();
}
public static void show(){
System.out.println("张三");
System.out.println("男");
System.out.println("20");
}
a. 使用输入法中的制表符在控制台直接打印出棋盘,然后寻找落子位置的特征
JAVA
System.out.println("┌────┬────┬────┬────┬────┬────┬────┬────┬────┐");
System.out.println("│ │ │ │ │ │ │ │ │ │");
System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");
System.out.println("│ │ │ │ │ │ │ │ │ │");
System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");
System.out.println("│ │ │ │ │ │ │ │ │ │");
System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");
System.out.println("│ │ │ │ │ │ │ │ │ │");
System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");
System.out.println("│ │ │ │ │ │ │ │ │ │");
System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");
System.out.println("│ │ │ │ │ │ │ │ │ │");
System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");
System.out.println("│ │ │ │ │ │ │ │ │ │");
System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");
System.out.println("│ │ │ │ │ │ │ │ │ │");
System.out.println("├────┼────┼────┼────┼────┼────┼────┼────┼────┤");
System.out.println("│ │ │ │ │ │ │ │ │ │");
System.out.println("└────┴────┴────┴────┴────┴────┴────┴────┴────┘");
b. 利用二维数组重新制作棋盘
JAVA
public class Gobang {
public static char[][] chessboard = {
{'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}
};
public static String separator = "─────";
public static void main(String[] args) {
System.out.println(" 0 1 2 3 4 5 6 7 8 9");
for(int i=0; i<chessboard.length; i++){ //外层循环控制行
System.out.print(i + " ");
for(int j=0; j<chessboard[i].length; j++){//内层循环控制列
if(j == chessboard[i].length - 1){ //最后一列
System.out.print(chessboard[i][j]);
} else {
System.out.print(chessboard[i][j] + separator);//使用
print打印,不换行
}
}
System.out.println();
if(i < chessboard.length -1){//排除最后一行
System.out.println(" │ │ │ │ │ │ │ │ │ │");
}
}
}
}
c. 棋盘在玩家使用过程中会反复展示,需要使用方法来进行优化
JAVA
public class Gobang {
public static char[][] chessboard = {
{'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}
};
public static String separator = "─────";
public static void main(String[] args) {
showChessboard();
}
public static void showChessboard(){
System.out.println(" 0 1 2 3 4 5 6 7 8 9");
for(int i=0; i<chessboard.length; i++){ //外层循环控制行
System.out.print(i + " ");
for(int j=0; j<chessboard[i].length; j++){//内层循环控制列
if(j == chessboard[i].length - 1){ //最后一列
System.out.print(chessboard[i][j]);
} else {
System.out.print(chessboard[i][j] + separator);//使用print打印,不换行
}
}
System.out.println();
if(i < chessboard.length -1){//排除最后一行
System.out.println(" │ │ │ │ │ │ │ │ │ │");
}
}
}
a. 玩家A、B会交替落子
JAVA
/**
* 五子棋
*/
public class Gobang {
public static char[][] chessboard = {
{'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}
};
public static String separator = "─────";
public static void main(String[] args) {
showChessboard();
int totalPosition = chessboard.length * chessboard[0].length;
for(int times=0; times < totalPosition; times++){
System.out.println(times % 2 == 0 ? "请玩家A落子:" : "请玩家B落子");
}
}
public static void showChessboard(){
System.out.println(" 0 1 2 3 4 5 6 7 8 9");
for(int i=0; i<chessboard.length; i++){ //外层循环控制行
System.out.print(i + " ");
for(int j=0; j<chessboard[i].length; j++){//内层循环控制列
if(j == chessboard[i].length - 1){ //最后一列
System.out.print(chessboard[i][j]);
} else {
System.out.print(chessboard[i][j] + separator);//使用print打印,不换行
}
}
System.out.println();
if(i < chessboard.length -1){//排除最后一行
System.out.println(" │ │ │ │ │ │ │ │ │ │");
}
}
}
}
b. 落子位置必须是0~100之间的整数,且不能使用已经存在棋子的位置
JAVA
import java.util.Scanner;
/**
* 五子棋
*/
public class Gobang {
public static char[][] chessboard = {
{'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}
};
public static String separator = "─────";
public static char pieceA = '○'; //玩家A的棋子
public static char pieceB = '■';//玩家B的棋子
public static void main(String[] args) {
showChessboard();
int totalPosition = chessboard.length * chessboard[0].length;
Scanner sc = new Scanner(System.in);
for(int times=0; times < totalPosition; times++){
System.out.println(times % 2 == 0 ? "请玩家A落子:" : "请玩家B落
子:");
while(true){//保证落子成功的一个循环
//检测Scanner中是否有输入的数据并且判断数据是否为整数,如果没有数据,
就需要从控制台输入
if(sc.hasNextInt()){
int position = sc.nextInt();
if(position >= 0 && position < totalPosition){
char currentPiece = (times % 2 == 0) ? pieceA :
pieceB;
// position = 21 13x13 21 / 13 = 1 21 % 13
= 8
int row = position / chessboard.length; //位置除以棋
盘数组的长度得到行号
int col = position % chessboard[0].length; //位置取模
棋盘数组的总列数得到列号
if(chessboard[row][col] == pieceA ||
chessboard[row][col] == pieceB){
System.out.println("非法落子,请重新选择落子位置");
continue;
} else {
chessboard[row][col] = currentPiece;
break;
}
} else {
System.out.println("非法落子,请重新选择落子位置");
}
} else {
System.out.println("非法落子,请重新选择落子位置");
sc.next();//将Scanner中存储的数据取出来,防止死循环
}
}
//落子完成后,棋盘需要重新展示
showChessboard();
}
}
public static void showChessboard(){
System.out.println(" 0 1 2 3 4 5 6 7
8 9");
for(int i=0; i<chessboard.length; i++){ //外层循环控制行
System.out.print(i + " ");
for(int j=0; j<chessboard[i].length; j++){//内层循环控制列
if(j == chessboard[i].length - 1){ //最后一列
System.out.print(chessboard[i][j]);
} else {
System.out.print(chessboard[i][j] + separator);//使用
print打印,不换行
}
}
System.out.println();
if(i < chessboard.length -1){//排除最后一行
System.out.println(" │ │ │ │ │ │
│ │ │ │");
}
}
}
}
c. 落子完成后,需要校验是否获胜
JAVA
import java.util.Scanner;
/**
* 五子棋
*/
public class Gobang {
public static char[][] chessboard = {
{'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}
};
public static String separator = "─────";
public static char pieceA = '○'; //玩家A的棋子
public static char pieceB = '■';//玩家B的棋子
public static void main(String[] args) {
showChessboard();
int totalPosition = chessboard.length * chessboard[0].length;
Scanner sc = new Scanner(System.in);
outer:
for(int times=0; times < totalPosition; times++){
System.out.println(times % 2 == 0 ? "请玩家A落子:" : "请玩家B落
子:");
char currentPiece = (times % 2 == 0) ? pieceA : pieceB;//当前使
用的棋子
while(true){//保证落子成功的一个循环
//检测Scanner中是否有输入的数据并且判断数据是否为整数,如果没有数
据,就需要从控制台输入
if(sc.hasNextInt()){
int position = sc.nextInt();
if(position >= 0 && position < totalPosition){
// position = 21 13x13 21 / 13 = 1 21 % 13
= 8
int row = position / chessboard.length; //位置除以棋
盘数组的长度得到行号
int col = position % chessboard[0].length; //位置取
模棋盘数组的总列数得到列号
if(chessboard[row][col] == pieceA ||
chessboard[row][col] == pieceB){
System.out.println("非法落子,请重新选择落子位置");
continue;
} else {
chessboard[row][col] = currentPiece;
break;
}
} else {
System.out.println("非法落子,请重新选择落子位置");
}
} else {
System.out.println("非法落子,请重新选择落子位置");
sc.next();//将Scanner中存储的数据取出来,防止死循环
}
}
//落子完成后,棋盘需要重新展示
showChessboard();
//判断获胜情况,一共4种
for(int i=0; i<chessboard.length; i++){
for(int j=0; j<chessboard[i].length; j++){
//第一种:水平方向上存在同一玩家的连续5颗棋子
//(i, j) (i, j+1) (i, j+2) (i, j+3) (i, j+4)
boolean case1 = (j + 4 < chessboard[i].length)
&& chessboard[i][j] == currentPiece
&& chessboard[i][j+1] == currentPiece
&& chessboard[i][j+2] == currentPiece
&& chessboard[i][j+3] == currentPiece
&& chessboard[i][j+4] == currentPiece;
//第二种:锤子方向上存在同一玩家的连续5颗棋子
//(i, j) (i+1, j) (i+2, j) (i+3, j) (i+4, j)
boolean case2 = (i + 4 < chessboard.length)
&& chessboard[i][j] == currentPiece
&& chessboard[i+1][j] == currentPiece
&& chessboard[i+2][j] == currentPiece
&& chessboard[i+3][j] == currentPiece
&& chessboard[i+4][j] == currentPiece;
//第三种:135°角上存在同一玩家的连续5颗棋子
//(i, j) (i+1, j+1) (i+2, j+2) (i+3, j+3) (i+4, j+4)
boolean case3 = (i + 4 < chessboard.length)
&& (j + 4 < chessboard[i].length)
&& chessboard[i][j] == currentPiece
&& chessboard[i+1][j+1] == currentPiece
&& chessboard[i+2][j+2] == currentPiece
&& chessboard[i+3][j+3] == currentPiece
&& chessboard[i+4][j+4] == currentPiece;
//第四种:45°角上存在同一玩家的连续5颗棋子
//(i, j) (i-1, j+1) (i-2, j+2) (i-3, j+3) (i-4, j+4)
boolean case4 = (i > 4) && (j + 4 <
chessboard[i].length)
&& chessboard[i][j] == currentPiece
&& chessboard[i-1][j+1] == currentPiece
&& chessboard[i-2][j+2] == currentPiece
&& chessboard[i-3][j+3] == currentPiece
&& chessboard[i-4][j+4] == currentPiece;
if(case1 || case2 || case3 || case4){
System.out.println(times % 2 == 0 ? "玩家A获得胜利"
: "玩家B获得胜利");
break outer;
}
}
}
}
}
public static void showChessboard(){
System.out.println(" 0 1 2 3 4 5 6 7
8 9");
for(int i=0; i<chessboard.length; i++){ //外层循环控制行
System.out.print(i + " ");
for(int j=0; j<chessboard[i].length; j++){//内层循环控制列
if(j == chessboard[i].length - 1){ //最后一列
System.out.print(chessboard[i][j]);
} else {
System.out.print(chessboard[i][j] + separator);//使用
print打印,不换行
}
}
System.out.println();
if(i < chessboard.length -1){//排除最后一行
System.out.println(" │ │ │ │ │ │
│ │ │ │");
}
}
}
}
d. 棋盘使用完毕还未分出胜负,需要提示
JAVA
import java.util.Scanner;
/**
* 五子棋
*/
public class Gobang {
public static char[][] chessboard = {
{'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}
};
public static String separator = "─────";
public static char pieceA = '○'; //玩家A的棋子
public static char pieceB = '■';//玩家B的棋子
public static int times = 0; //记录棋盘使用次数,偶数次玩家A落子,奇数次玩家B落子
public static void main(String[] args) {
showChessboard();
int totalPosition = chessboard.length * chessboard[0].length;
Scanner sc = new Scanner(System.in);
outer:
while (times < totalPosition){
System.out.println(times % 2 == 0 ? "请玩家A落子:" : "请玩家B落
子:");
char currentPiece = (times % 2 == 0) ? pieceA : pieceB;//当前使
用的棋子
while(true){//保证落子成功的一个循环
//检测Scanner中是否有输入的数据并且判断数据是否为整数,如果没有数据,就需要从控制台输入
if(sc.hasNextInt()){
int position = sc.nextInt();
if(position >= 0 && position < totalPosition){
// position = 21 13x13 21 / 13 = 1 21 % 13 = 8
int row = position / chessboard.length; //位置除以棋
盘数组的长度得到行号
int col = position % chessboard[0].length; //位置取
模棋盘数组的总列数得到列号
if(chessboard[row][col] == pieceA ||
chessboard[row][col] == pieceB){
System.out.println("非法落子,请重新选择落子位置");
continue;
} else {
chessboard[row][col] = currentPiece;
break;
}
} else {
System.out.println("非法落子,请重新选择落子位置");
}
} else {
System.out.println("非法落子,请重新选择落子位置");
sc.next();//将Scanner中存储的数据取出来,防止死循环
}
}
//落子完成后,棋盘需要重新展示
showChessboard();
//判断获胜情况,一共4种
for(int i=0; i<chessboard.length; i++){
for(int j=0; j<chessboard[i].length; j++){
//第一种:水平方向上存在同一玩家的连续5颗棋子
//(i, j) (i, j+1) (i, j+2) (i, j+3) (i, j+4)
boolean case1 = (j + 4 < chessboard[i].length)
&& chessboard[i][j] == currentPiece
&& chessboard[i][j+1] == currentPiece
&& chessboard[i][j+2] == currentPiece
&& chessboard[i][j+3] == currentPiece
&& chessboard[i][j+4] == currentPiece;
//第二种:锤子方向上存在同一玩家的连续5颗棋子
//(i, j) (i+1, j) (i+2, j) (i+3, j) (i+4, j)
boolean case2 = (i + 4 < chessboard.length)
&& chessboard[i][j] == currentPiece
&& chessboard[i+1][j] == currentPiece
&& chessboard[i+2][j] == currentPiece
&& chessboard[i+3][j] == currentPiece
&& chessboard[i+4][j] == currentPiece;
//第三种:135°角上存在同一玩家的连续5颗棋子
//(i, j) (i+1, j+1) (i+2, j+2) (i+3, j+3) (i+4, j+4)
boolean case3 = (i + 4 < chessboard.length)
&& (j + 4 < chessboard[i].length)
&& chessboard[i][j] == currentPiece
&& chessboard[i+1][j+1] == currentPiece
&& chessboard[i+2][j+2] == currentPiece
&& chessboard[i+3][j+3] == currentPiece
&& chessboard[i+4][j+4] == currentPiece;
//第四种:45°角上存在同一玩家的连续5颗棋子
//(i, j) (i-1, j+1) (i-2, j+2) (i-3, j+3) (i-4, j+4)
boolean case4 = (i > 4) && (j + 4 < chessboard[i].length)
&& chessboard[i][j] == currentPiece
&& chessboard[i-1][j+1] == currentPiece
&& chessboard[i-2][j+2] == currentPiece
&& chessboard[i-3][j+3] == currentPiece
&& chessboard[i-4][j+4] == currentPiece;
if(case1 || case2 || case3 || case4){
System.out.println(times % 2 == 0 ? "玩家A获得胜利": "玩家B获得胜利");
break outer;
}
}
}
times++;
}
if(times == totalPosition){//说明棋盘已经用完还未分出胜负
System.out.println("平均");
}
}
public static void showChessboard(){
System.out.println(" 0 1 2 3 4 5 6 7 8 9");
for(int i=0; i<chessboard.length; i++){ //外层循环控制行
System.out.print(i + " ");
for(int j=0; j<chessboard[i].length; j++){//内层循环控制列
if(j == chessboard[i].length - 1){ //最后一列
System.out.print(chessboard[i][j]);
} else {
System.out.print(chessboard[i][j] + separator);//使用print打印,不换行
}
}
System.out.println();
if(i < chessboard.length -1){//排除最后一行
System.out.println(" │ │ │ │ │ │ │ │ │ │");
}
}
}
}
为落子、非法落子及获胜添加音效
JAVA
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
import java.util.Scanner;
/**
* 五子棋
*/
public class Main {
public static char[][] chessboard = {
{'┌','┬','┬','┬','┬','┬','┬','┬','┬','┐'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'├','┼','┼','┼','┼','┼','┼','┼','┼','┤'},
{'└','┴','┴','┴','┴','┴','┴','┴','┴','┘'}
};
public static String separator = "─────";
public static char pieceA = '○'; //玩家A的棋子
public static char pieceB = '■';//玩家B的棋子
public static int times = 0; //记录棋盘使用次数,偶数次玩家A落子,奇数次玩家B落子
public static void main(String[] args) {
showChessboard();
int totalPosition = chessboard.length * chessboard[0].length;
Scanner sc = new Scanner(System.in);
outer:
while (times < totalPosition){
System.out.println(times % 2 == 0 ? "请玩家A落子:" : "请玩家B落子:");
char currentPiece = (times % 2 == 0) ? pieceA : pieceB;//当前使用的棋子
while(true){//保证落子成功的一个循环
//检测Scanner中是否有输入的数据并且判断数据是否为整数,如果没有数据,就需要从控制台输入
if(sc.hasNextInt()){
int position = sc.nextInt();
if(position >= 0 && position < totalPosition){
// position = 21 13x13 21 / 13 = 1 21 % 13= 8
int row = position / chessboard.length; //位置除以棋盘数组的长度得到行号
int col = position % chessboard[0].length; //位置取模棋盘数组的总列数得到列号
if(chessboard[row][col] == pieceA ||
chessboard[row][col] == pieceB){
System.out.println("非法落子,请重新选择落子位置");
playAudio("illegal.wav");
continue;
} else {
chessboard[row][col] = currentPiece; //落子
playAudio("fill.wav");
break;
}
} else {
System.out.println("非法落子,请重新选择落子位置");
playAudio("illegal.wav");
}
} else {
System.out.println("非法落子,请重新选择落子位置");
playAudio("illegal.wav");
sc.next();//将Scanner中存储的数据取出来,防止死循环
}
}
//落子完成后,棋盘需要重新展示
showChessboard();
//判断获胜情况,一共4种
for(int i=0; i<chessboard.length; i++){
for(int j=0; j<chessboard[i].length; j++){
//第一种:水平方向上存在同一玩家的连续5颗棋子
//(i, j) (i, j+1) (i, j+2) (i, j+3) (i, j+4)
boolean case1 = (j + 4 < chessboard[i].length)
&& chessboard[i][j] == currentPiece
&& chessboard[i][j+1] == currentPiece
&& chessboard[i][j+2] == currentPiece
&& chessboard[i][j+3] == currentPiece
&& chessboard[i][j+4] == currentPiece;
//第二种:锤子方向上存在同一玩家的连续5颗棋子
//(i, j) (i+1, j) (i+2, j) (i+3, j) (i+4, j)
boolean case2 = (i + 4 < chessboard.length)
&& chessboard[i][j] == currentPiece
&& chessboard[i+1][j] == currentPiece
&& chessboard[i+2][j] == currentPiece
&& chessboard[i+3][j] == currentPiece
&& chessboard[i+4][j] == currentPiece;
//第三种:135°角上存在同一玩家的连续5颗棋子
//(i, j) (i+1, j+1) (i+2, j+2) (i+3, j+3) (i+4, j+4)
boolean case3 = (i + 4 < chessboard.length)
&& (j + 4 < chessboard[i].length)
&& chessboard[i][j] == currentPiece
&& chessboard[i+1][j+1] == currentPiece
&& chessboard[i+2][j+2] == currentPiece
&& chessboard[i+3][j+3] == currentPiece
&& chessboard[i+4][j+4] == currentPiece;
//第四种:45°角上存在同一玩家的连续5颗棋子
//(i, j) (i-1, j+1) (i-2, j+2) (i-3, j+3) (i-4, j+4)
boolean case4 = (i > 4) && (j + 4 <
chessboard[i].length)
&& chessboard[i][j] == currentPiece
&& chessboard[i-1][j+1] == currentPiece
&& chessboard[i-2][j+2] == currentPiece
&& chessboard[i-3][j+3] == currentPiece
&& chessboard[i-4][j+4] == currentPiece;
if(case1 || case2 || case3 || case4){
System.out.println(times % 2 == 0 ? "玩家A获得胜利"
: "玩家B获得胜利");
playAudio("win.wav");
break outer;
}
}
}
times++;
}
if(times == totalPosition){//说明棋盘已经用完还未分出胜负
System.out.println("平均");
}
}
public static void showChessboard(){
System.out.println(" 0 1 2 3 4 5 6 7 8 9");
for(int i=0; i<chessboard.length; i++){ //外层循环控制行
System.out.print(i + " ");
for(int j=0; j<chessboard[i].length; j++){//内层循环控制列
if(j == chessboard[i].length - 1){ //最后一列
System.out.print(chessboard[i][j]);
} else {
System.out.print(chessboard[i][j] + separator);//使用print打印,不换行
}
}
System.out.println();
if(i < chessboard.length -1){//排除最后一行
System.out.println(" │ │ │ │ │ │ │ │ │ │ ");
}
}
}
/**
* 播放声音
* @param fileName
*/
public static void playAudio(String fileName){
URL url = Main.class.getResource(fileName);
AudioClip clip = Applet.newAudioClip(url);
clip.play();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}