Java第一阶段(5)【 数组(二)+ 方法 】 11.12

二维数组

  • 定义

如果一个数组中全是一维数组,那么这个数组就是二维数组

  • 创建二维数组

    • 静态初始化

整合写:

int[][] arr = { {1,2,3} , {4,5,6} , {7,8,9} , {10,11,12} };

分步写:

int[] arr1 = {1,2,3};
int[] arr2 = {4,5,6};
int[] arr3 = {7,8,9};
int[] arr4 = {10,11,12};
int[][] array = {arr1, arr2, arr3, arr4 }; 
  • 如何识别二维数组中的元素、角标
数组和角标 输出值
System.out.println(“二位数组:” + array); [[I@7852e922
System.out.println(“二位数组中的第一个一维数组:” + array[0]); [I@4e25154f
System.out.println(“二位数组中的第一个一维数组:” + array[1][1]); 5
System.out.println(“二位数组中的第一个一维数组:” + array[2][1]); 8
  • 二维数组的遍历过程
二维数组
一维数组
一维数组里具体的数字

外层循环遍历:一维数组;
内层数组遍历:一维数组中的元素;

  • 格式
		for (int i = 0; i < array.length; i++) {			//二维数组中有几个一维数组
			for (int j = 0; j < array[i].length; j++) {		//一维数组的长度
				System.out.print(array[i][j] + " ");
			}
		}
  • 动态初始化

格式

   int[][] arr = new int[3][4];
   //[3]:代表二维数组中有3个一维数组
   //[4]:每个一维数组中有4个数字

方法

  • 格式
修饰符 返回类型(有返回值写返回数据类型,没返回值写void) 方法名 (参数列表)
//参数列表(没有参数的情况可以不写):类型名字 取个名字
{
方法体......
//如果这个方法有返回值类型 最后一行写return
return + 对应返回值类型的值;
}
  • 方法定义的位置
    不能在别的方法里,必须定义在其他同级的位置,定义在类里面
  • 方法的作用
  1. 简化代码(过程型方法)
  2. 代码反复出现 ,提高代码复用性(功能型方法)
  • 创建方法
  1. 无返回值、无参数
//方法welcome
public static void welcome(){
	System.out.println("hello world");
}
//主方法
public static void main(String[] args){
	//调用welcome方法
	welcome();
}
  1. 有返回值、有参数
//方法add
public static int add(int a ,int b){
	int c = a+b;
	return c;
}
//主方法
public static void main(String[] args){
    int x = 20;
	int y = 10;
	//调用add方法,把方法return的值赋值给sum
	int sum = add(x,y);
	//输出x+y
	System.out.println(sum);
}
  • 方法中的元素
    参数列表中的参数:
    形式参数 形参:写方法的时候
    实际参数 实参:实际传入方法参数列表的参数

快捷键

  • 快捷导包:ctrl+shift+o

练习

  1. 在main方法中根据键盘输入一个1-100之间的数字n,另外写一个方法showLove,传入这个n,循环n次"我爱你"
package test;
public class ShowLove {
	public static void main(String[] args) {
		showLove(6);
	}
	public static void showLove(int a) {
		for (int i = 0; i < a; i++) {
			System.out.println("我爱你");
		}
	}
}
  1. 在main方法中随便定义一个字符串,另外写一个方法printString,传入这个字符串,此方法专门负责将字符串打印出来。
package test;
public class PrintString {
   public static void main(String[] args) {
   	printString("我是字符串");
   }

   public static void printString(String a) {
   	System.out.println(a);
   }
}
  1. 在main方法中定义一个数组,写一个方法showIntArr,专门用来遍历int类型的一维数组。
package test;
public class ShowIntArr {
   public static void main(String[] args) {
   	int[] array = { 1, 2, 3 };
   	showIntArr(array);
   }

   public static void showIntArr(int[] arr) {
   	for (int i = 0; i < arr.length; i++) {
   		System.out.print(arr[i] + " ");
   	}
   	System.out.println();
   }
}
  1. 写一个方法showCharArr,专门用来遍历char类型的一维数组。
package test;
public class ShowCharArr {
   public static void main(String[] args) {
   	char[] cha = { '我', '是', '字', '符' };
   	showCharArr(cha);
   }

   public static void showCharArr(char[] cha) {
   	for (int i = 0; i < cha.length; i++) {
   		System.out.print(cha[i] + " ");
   	}
   }
}
  1. 写一个方法showStringArr,专门用来遍历String类型的一维数组。
package test;
public class ShowStringArr {
   public static void main(String[] args) {
   	String[] str = { "我是", "字符", "串" };
   	showStringArr(str);
   }

   public static void showStringArr(String[] a) {
   	for (int i = 0; i < a.length; i++) {
   		System.out.println(a[i]);
   	}
   }
}
  1. 写一个方法专门showCharArr2,用来遍历char类型的二维数组
package test;
public class ShowCharArr2 {
   public static void main(String[] args) {
   	char[][] ch = { { '1', '2' }, { '3', '4' } };
   	showCharArr2(ch);
   }

   public static void showCharArr2(char[][] cha) {
   	for (int i = 0; i < cha.length; i++) {
   		for (int j = 0; j < cha[i].length; j++) {
   			System.out.print(cha[i][j] + " ");
   		}
   		System.out.println();
   	}
   }
}
  1. 在main方法中有一个int类型一维数组,写一个方法getMax,用于获取数组中的最大值,调用方法之后将返回的最大值赋值给一个变量a,把a打印出来。
package test;
public class GetMax {
   public static void main(String[] args) {
   	int[] array = { 1, 2, 3, 4, 5 };
   	int a = getMax(array);
   	System.out.println("最大的值为" + a);
   }

   public static int getMax(int[] arr) {
   	int max = 0;
   	for (int i = 0; i < arr.length; i++) {
   		if (arr[i] > max) {
   			max = arr[i];
   		}
   	}
   	return max;
   }
}
  1. 根据键盘输入一个数字(1-7),写一个方法getWeek,传入这个数字,专门来获取这个数字所对应的星期几字符串,之后调用这个方法来获取返回的字符串并赋值给s,打印出这个s。
package test;
import java.util.Scanner;
public class GetWeek {
   public static void main(String[] args) {

   	System.out.println("输入一个数字(1-7)");
   	Scanner ss = new Scanner(System.in);
   	int a = ss.nextInt()-1;
   	int s = getWeek(a);
   	System.out.println("传出的s为:"+s);
   }

   public static int getWeek(int b) {
   	String[] week = {"星期一","星期二","星期三","星期四","星期五","星期六","星期七"};
   	System.out.println("今天为" + week[b]);
   	return b;
   	
   }
}
  1. 在main方法中定义一个int类型的一维数组,根据键盘输入一个数字,写一个方法addNum,传入这个数字和数组,把这个数字加到这个数组的末尾。之后在main方法中调用addNum的方法把返回出来的数组赋值给原来的数组,并遍历原来的数组。
package test;
import java.util.Scanner;
public class AddNum {
   public static void main(String[] args) {
   int[] array = {  1,2,3};
   System.out.println("请输入一个数字:");
   	int key = s.nextInt();
   	
   	array =   addNum( array,key );
   	System.out.println("遍历原数组:");
   	for (int i = 0; i < array.length; i++) {
   		System.out.println(array[i]);
   	}
   }

   public static int[] addNum(int[] arr, int a) {
   //创建一个新的数组 比 传进来的数组的 长度 还 多  1
   	int[] arr2 = new int[arr.length + 1];
   	//把传进来的数组的 所有的元素 赋值给 新数组对应的位置
   	for (int i = 0; i < arr.length; i++) {
   		arr2[i] = arr[i];
   	}
   	//把新数组的最后一位 赋值为  传进来num
   	arr2[arr.length-1] = a;
   	return arr2;
   }
}
  1. 写一个方法sortMethod1,用于对int类型一维数组进行选择排序;写一个方法sortMethod2,用于对int类型一维数组进行冒泡排序。
package test;

public class SortMethod1 {
   public static void main(String[] args) {
   	int[] arr = { 3, 4, 2, 5, 1 };
   	
   	System.out.println("选择排序的结果:");
   	sortMethod1(arr);
   	System.out.println();
   	System.out.println("--------------------");
   	System.out.println("冒泡排序的结果:");
   	sortMethod2(arr);
   	System.out.println();
   }

   public static void sortMethod1(int[] arr) {
   	// 选择排序
   	int temp;
   	// 两层循环    内层循环每次 得到一个最大值    外层循环  决定最大值要拿多少次 
   	for (int i = 0; i < arr.length-1; i++) {
   		for (int j = i+1; j < arr.length; j++) {
   		//如果 你  j  比我 i   大  我就跟你换位 
   			if (arr[j] > arr[i]) {
   			//交换两个数
   				temp = arr[j];
   				arr[j] = arr[i];
   				arr[i] = temp;
   			}
   		}
   	}
   	for (int i = 0; i < arr.length; i++) {
   		System.out.print(arr[i] + " ");
   	}
   }
   // 冒泡排序
   public static void sortMethod2(int[] arr){
   	for (int i = 0; i < arr.length; i++) {
   		for (int j = 0; j < arr.length-i-1; j++) {
   			if(arr[j]>arr[j+1]){
   				int temp = arr[j+1];
   				arr[j+1]=arr[j];
   				arr[j] = temp;
   			}
   		}
   	}
   	for(int i = 0 ;i<arr.length;i++){
   		System.out.print(arr[i] +" ");
   	}
   }
}
  1. 二维打地鼠
    1. 打包4个方法:欢迎的方法,遍历地洞数组的方法,随机出现敌人的方法,显示游戏结束统计的方法。 1. 打包4个方法:欢迎的方法,遍历地洞数组的方法,随机出现敌人的方法,显示游戏结束统计的方法。
    2. 初始化char类型的二维数组,每个洞都是 O
    3. 游戏循环10次
    4. 地鼠随机出现在二维数组中的某个元素位置,X
    5. 提示玩家击打
    6. 判断是否打中
    7. 击中得2分,未击中扣1分
    8. 游戏结束,统计击中次数,未击中次数 和最后的得分。
    9. 按99重新开始(数据还原),按88退出游戏
package test;

import java.util.Random;
import java.util.Scanner;

public class Home3 {
  
  
  public static void main(String[] args) {
  	
  	//定义变量
  	Scanner s = new Scanner(System.in);
  	int hit = 0;
  	int miss = 0;
  	int score = 0 ;
  	int count = 0 ;
  	//创建一个洞穴的二维数组  char
  	char[][]  chs = new char[4][5];
  	welcome();
  	//初始化
  	init(chs);
  	System.out.println("洞穴初始化中、、、");
  	showHoles(chs);
  	
  	//循环游戏
  	while(count<10){
  		count++;
  		System.out.println("第"+count+"次,游戏:");
  		
  		//地鼠出现    随机的行与列
  		int[] datas =  ranEnemy(chs);
  		chs[  datas[0]  ][  datas[1]  ] = 'X';
  		System.out.println("请注意,地鼠出现了!!!");
  		showHoles(chs);
  		System.out.println("请输入对应的行与列 进行 击打:(用回车隔开) 99-重新开始   88-退出游戏");
  		int pr = s.nextInt()-1;
  		
  		
  		if(pr==87){
  			System.out.println("谢谢再见!!!");
  			//  break continue return
  			return;
  		}
  		
  		if(pr== 98 ){
  			System.out.println("游戏即将重新开始。。。");
  			//  分数  击中 未击中 次数  洞穴  
  			score = 0;
  			hit = 0;
  			miss= 0;
  			count=0;
  			chs[  datas[0]  ][  datas[1]  ] ='O';
  			
  			continue;
  		}
  		
  		int pc = s.nextInt()-1;
  		
  		//判断
  		if( pr==datas[0] && pc == datas[1]  ){
  			System.out.println("恭喜你打中了!");
  			hit++;
  			score+=2;
  		}else{
  			System.out.println("很遗憾,没打中。。。");
  			miss++;
  			score--;
  		}
  		
  		//还原
  		chs[  datas[0]  ][  datas[1]  ] ='O';
  		System.out.println("当前得分:"+ score);

  	}
  	
  	//游戏结束
  	over( hit,miss,score  );
  }
  
  //初始化洞穴的方法   空格--》O
  public static void init( char[][] arr  ){
  	for (int i = 0; i < arr.length; i++) {
  		for (int j = 0; j < arr[i].length; j++) {
  			arr[i][j] = 'O';
  		}
  	}
  	
  }
  //欢迎
  public static void welcome(){
  	
  	System.out.println("*************");
  	System.out.println("欢迎来到打地鼠游戏");
  	System.out.println("*************");
  
  }
  
  //char[][] 遍历地洞的方法
  
  public static void showHoles( char[][]  arr ){
  	
  	for (int i = 0; i < arr.length; i++) {
  		for (int j = 0; j < arr[i].length; j++) {
  			System.out.print(arr[i][j]+" ");
  		}
  		System.out.println();
  	}
  	
  }
  
  //随机出现敌人   随机 的 是两个 角标    行int  列int      
  public static int[] ranEnemy(char[][]  arr){
  	
  	//随机
  	Random r= new Random();
  	// 外层 --行 -- 二维数组的 长度
  	int row = r.nextInt(arr.length);
  	int col = r.nextInt(arr[row].length);//  刚才那行的某一列
  	
  	int[] datas = { row,col};
  	
  	return datas;
  }
  //游戏结束    a击中   b未击中   c 分数
  public static void over( int a , int b, int c  ){
  	System.out.println("**************");
  	System.out.println("game over");
  	System.out.println("击中:"+ a);
  	System.out.println("未击中:"+ b);
  	System.out.println("最终得分:["+ c+"]");
  }
}

猜你喜欢

转载自blog.csdn.net/qq_42829628/article/details/83999157
今日推荐