第三章——数组
一、数组的创建
1.数组的声明
数据类型 [] 数组名字
int[] a;
数据类型 数组的名字
int a[];
2.数组的初始化
int[] a=new int[5];
int[] a=new int[]{1,2,3,4,5};
int[] a={1,2,3,4,5};
3.数组元素的访问
数组名[索引]
a[0] a[1]
4.获取数组长度
数组名.length
5.数组迭代的两种方式
(1)for循环
int[] b=new int[]{1,2,3,4,5,6,7}
for(int i=0;i<b.length;i++){
System.out.println(b[i]);
}
(2)增强for循环
public class project1 {
public static void main(String[] args) {
int[] a={1,2,3,4,5};
//增强for循环
for(int t:a){//用来接收每次循环打印元素的临时变量
System.out.println(t);
}
}
}
二、数组的排序
1、冒泡排序
public class project2 {
public static void main(String[] args) {
//冒泡排序
int[] a={5,4,3,2,1};
for(int i = 0;i<a.length-1;i++){
for(int j = 0;j<a.length-1-i;j++){
if(a[j]>a[j+1]){
int temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.println(Arrays.toString(a));
}
}
2、选择排序
import java.util.Arrays;
public class project3 {
public static void main(String[] args) {
//选择排序
int[] a={5,4,3,2,1};
for(int i=0;i<a.length-1;i++){
int minindex=i;
for(int j=i+1;j<a.length;j++){
if(a[minindex]>a[j]){
minindex=j;
}
}
int temp=a[minindex];
a[minindex]=a[i];
a[i]=temp;
}
System.out.println(Arrays.toString(a));
}
}
三、二维数组
1.杨辉三角
import java.util.Arrays;
public class project5 {
public static void main(String[] args) {
//杨辉三角
int[][] a = new int[10][19];
a[0][9] = 1;
for (int i = 1; i < a.length - 1; i++) {
for (int j = 1; j < a[i].length - 1; j++) {
a[i][j] = a[i - 1][j - 1] + a[i - 1][j + 1];
}
}
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a[i].length - 1; j++) {
if (a[i][j] == 0) {
System.out.print("\t");
}else{
System.out.print(a[i][j] + "\t");
}
}
System.out.println();
}
}
}
四、简易的五子棋游戏
public class startWZQ {
public static void main(String[] args) {
WZQ.startGame();
WZQ.play();
WZQ.print();
}
}
import java.util.Scanner;
public class WZQ {
/*
1.提供一个启动五子棋游戏的方法
初始化棋盘方法
打印棋盘方法
2.开始下棋,黑白棋交替下棋
3.判断坐标是否合法,是否重复
4.判断输赢
*/
static String white = "☆";
static String black = "★";
static String[][] qp = new String[15][15];
static String[] num = {"⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "⒒", "⒓", "⒔", "⒕", "⒖"};
static String line = "十";
//启动五子棋游戏
public static void startGame() {
init();
print();
}
//判断是否重复或越界
public static boolean PANDUAN(int h, int l) {
if (h < 0 || l < 0 || h >= qp.length || l >= qp.length) {//判断是否越界
return false;
}
if (qp[h][l] != line) {//判断是否重复
return false;
}
return true;
}
//判断输赢
public static boolean isWin(int h, int l, String qz) {
//水平判断输赢
int spsum = 1;
//水平向左找
for (int leftl = l-2 ; leftl >= 0; leftl--) {
if (qp[h][leftl] == qz) {
spsum++;
} else {
break;
}
}
if (spsum == 5) {
return true;//ture——胜利
}
//水平向右找
for (int rightl = l + 1; rightl < qp[0].length - 1; rightl++) {
if (qp[h][rightl] == qz) {
spsum++;
} else {
break;
}
}
if (spsum == 5) {
return true;
}
//水平向下找
for(int lowh=h+1;lowh<qp[0].length-1;lowh++) {
if (qp[lowh][l] == qz) {
spsum++;
} else {
break;
}
}
if (spsum == 5) {
return true;
}
//水平向上找
for (int highh=h-1;highh>=0;highh--){
if(qp[highh][l]==qz){
spsum++;
}else {
break;
}
}
if (spsum == 5) {
return true;
}
//斜向右下
for(int rightl=l+1;rightl<qp[0].length-1;rightl++){
for (int lowh=rightl;lowh<qp[0].length-1;lowh++) {
if (qp[lowh][rightl] == qz) {
spsum++;
} else {
break;
}
}
}
if(spsum==5){
return true;
}
//斜向左上
for (int leftl=l-1;leftl>=0;leftl--){
for (int highh=leftl;highh>=0;highh--){
if(qp[highh][leftl]==qz){
spsum++;
}else{
break;
}
}
}
if(spsum==5){
return true;
}
//斜向右上
for(int rightl=l+2;rightl<qp[0].length-1;rightl++){
for(int highh=h-(rightl-l);highh>=0;highh--){
if(qp[highh][rightl]==qz){
spsum++;
}else {
break;
}
}
}
if(spsum==5){
return true;
}
//斜向左下
for (int leftl=l-2;leftl>=0;leftl--){
for (int lowh=h+(l-leftl);lowh<qp[0].length-1;lowh++){
if(qp[lowh][leftl]==qz){
spsum++;
}else {
break;
}
}
}
if(spsum==5){
return true;
}
return false;//false——没有胜利
}
//开始游戏
public static void play(){
System.out.println("欢迎来到游戏!");
boolean flag=true;
Scanner scanner=new Scanner(System.in);
while (true){
if(flag){
System.out.println("黑方下棋");
System.out.println("输入行:");
int h=scanner.nextInt()-1;
System.out.println("输入列:");
int l=scanner.nextInt()-1;
//判断是否重复或越界
boolean panduan=PANDUAN(h,l);
if(panduan){
qp[h][l]=black;
print();
boolean iswin=isWin(h,l,black);
if(iswin){
System.out.println("黑棋胜利");
break;
}
flag=false;
}else {
System.out.println("坐标越界或重复");
}
}else {
System.out.println("白方下棋");
System.out.println("输入行:");
int h=scanner.nextInt()-1;
System.out.println("输入列:");
int l=scanner.nextInt()-1;
boolean panduan=PANDUAN(h,l);
if(panduan){
qp[h][l]=white;
print();
boolean iswin=isWin(h,l,white);
if(iswin){
System.out.println("白棋胜利");
break;
}
flag=true;
}else {
System.out.println("坐标重复或越界");
}
}
}
}
//初始化棋盘
public static void init(){
Scanner scanner=new Scanner(System.in);
for(int i=0;i<qp.length;i++){
for(int j=0;j<qp[i].length;j++){
qp[i][j]=line;
if(j==qp[i].length-1){
qp[i][j]=num[i];
}if(i==qp.length-1){
qp[i][j]=num[j];
}
}
}
}
//打印棋盘
public static void print(){
for(int i=0;i<qp.length;i++){
for(int j=0;j<qp[i].length;j++){
System.out.print(qp[i][j]+"\t");
}
System.out.println();
}
}
}