JAVA方法的设计和练习
- 方法的设计
- 练习
- 1.设计一个方法 用来交换两个数组元素 a{1,2,3,4} b{5,6,7,8}
- 2.设计一个方法 用来交换一个数组(头尾互换)
- 3.设计一个方法 用来寻找数组中的极值(最大值 或 最小值)
- 4.设计一个方法 用来找寻给定的元素是否在数组内存在(Scanner输入一个)
- 5.设计一个方法 用来合并两个数组
- 6.设计一个方法 用来将一个数组按照最大值位置拆分
- 7.设计一个方法 用来去掉数组中的0元素
- 8.设计一个方法 用来存储给定范围内的素数(2-100) 素数在自然数之内
- 9.设计一个方法 用来给数组元素排序(冒泡排序算法) 既能升序又能降序
- 10.设计一个方法 用来实现用户登录认证(二维数组当作小数据库)
方法的设计
- 设计一个方法 用来画星星(控制台输出) 只输出4行 每一行4颗星星
分析 做事情之前是否需要提供条件 不需要
做事情之后是否需要留下结果 不需要 - 设计一个方法 用来画星星 只输出4行 直角三角形 第一行一颗 第二行两颗 第三行三颗
分析 做事情需要提供条件 不需要
做事情之后是否需要结果 不需要 - 设计一个方法 用来画星星 直角三角形 不一定画几行?
分析 做事情之前是否需要提供条件? int line
结果 不需要 - 设计一个方法 用来画星星 直角三角形 (反向) 几行不确定?
分析 是否提供条件? int line
结果 不需要 - 设计一个方法 用来画星星 几行不确定 直角三角形(方向也不确定)
分析 是否需要条件? int line boolean false
返回值 不需要
public class Person {
/**
* 设计一个方法 用来画星星(控制台输出) 只输出4行 每一行4颗星星
* 分析 做事情之前是否需要提供条件 不需要
* 做事情之后是否需要留下结果 不需要
*/
public void drawStar1(){
//控制行数
for (int i = 1; i <=4 ; i++) {
//控制每行的数量
for (int j = 1; j <=4 ; j++) {
//输出 * 号
System.out.print("*");
}
//输出换行
System.out.println();
}
}
/**
* 设计一个方法 用来画星星 只输出4行 直角三角形 第一行一颗 第二行两颗 第三行三颗
* 分析 做事情需要提供条件 不需要
* 做事情之后是否需要结果 不需要
*/
public void drawStar2(){
//控制行数 4
for (int i = 1; i <=4 ; i++) {
//控制输出 *
for ( int j = 1; j <=i ; j++) {
System.out.print("*");
}
System.out.println();
}
}
/**
* 设计一个方法 用来画星星 直角三角形 不一定画几行?
* 分析 做事情之前是否需要提供条件? int line
* 结果 不需要
*/
public void drawStar3(int line){
//控制行数 line
for (int i = 1; i <=line ; i++) {
//控制输出 *
for ( int j = 1; j <=i ; j++) {
System.out.print("*");
}
System.out.println();
}
}
/**
* 设计一个方法 用来画星星 直角三角形 (反向) 几行不确定?
* 分析 是否提供条件? int line
* 结果 不需要
*/
public void drawStar4(int line){
//控制行数 line
for (int i = 1; i <=line ; i++) {
//反向 前面要先输出空格
for ( int j = 1;j <=line-i ; j++) {
System.out.print(" ");
}
//空格输出完后 控制输出 *
for ( int j = 1; j <=i ; j++) {
System.out.print("*");
}
System.out.println();
}
}
/**
* 设计一个方法 用来画星星 几行不确定 直角三角形(方向也不确定)
* 分析 是否需要条件? int line boolean f
* f-->表示方向的意思 f==true 偏左 没有空格 f==false 偏右 带空格
*/
public void drawStar5(int line ,boolean f){
//控制行数 line
for (int i = 1; i <=line ; i++) {
//控制方向 f =true 时 正方向 f = false时反方向
if(!f){ //反向 前面要先输出空格
for ( int j = 1;j <=line-i ; j++) {
System.out.print(" ");
}
}
//空格输出完后 控制输出 *
for ( int j = 1; j <=i ; j++) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) {
Person p=new Person();
/* p.drawStar1();
System.out.println("===============================");
p.drawStar2();
System.out.println("===============================");
p.drawStar3(8);
System.out.println("===============================");
p.drawStar4(6);
System.out.println("===============================");
*/
p.drawStar5(6,false);
}
}
练习
1.设计一个方法 用来交换两个数组元素 a{1,2,3,4} b{5,6,7,8}
public class Demo{
/**
* 设计一个方法 用来交换两个数组元素 a{1,2,3,4} b{5,6,7,8}
* 分析 两个数组长度一样时 参数 int array1 int rray2
* 返回值 无
*/
public void swapArrayElements1(int[] array1, int[] array2) {
/**
* 方式一 将两个数组内的元素对应位置互换
* 方式一的设计问题在于
* 用循环的方式挨个交换数组内的元素 性能比较慢
* 交换的时候需要保证两个数组的长度是一致的
*/
for (int i = 0; i < array2.length; i++) { //每一次找到一个数组中的元素 跟另一个数组对应位置
array1[i] = array1[i] ^ array2[i];
array2[i] = array1[i] ^ array2[i];
array1[i] = array1[i] ^ array2[i];
}
}
/**
* 设计一个方法 用来交换两个数组元素 a{1,2,3,4} b{5,6,7,8,9,10}
* 分析 两个数组长度不一样时 参数 int a int rray2
* 返回值 需要
*/
public int[][] swapArrayElements2(int[] a, int[] b) {
//方法1 将两个数组的地址引用直接互换
/* int [] temp =a;
a=b;
b=temp;
int[][] result={a,b};
*/
int[][] result = {b, a};
return result;
}
public static void main(String[] args) {
Demo d = new Demo();
int[] x = new int[]{1, 2, 3, 4};
int[] y = new int[]{5, 6, 7, 8, 9, 10};
int[][] result =d.swapArrayElements2(x, y);
x = result[0];
y = result[1];
for (int i : x) {
System.out.println(i);
}
for (int i : y) {
System.out.println(i);
}
}
}
方式一
方式二
2.设计一个方法 用来交换一个数组(头尾互换)
public class TestFunctions {
/**
* 设计一个方法 用来交换一个数组(头尾互换)
* 分析 参数 一个数组
* 返回值 无
*/
public void changeArrayElements (int[] array) {
for (int i = 0; i < array.length/2; i++) {
//方式一
int temp =array[i];
array[i]= array[array.length-i-1];
array[array.length-i-1]=temp;
/* 方式二
array[i] =array[i] ^ array[array.length-i-1];
array[array.length-i-1] =array[i] ^ array[array.length-i-1];
array[i] =array[i] ^ array[array.length-i-1];*/
}
}
public static void main(String[] args) {
//交换数组内部元素
TestFunctions tf = new TestFunctions();
//1.有一个数组
int[] x = new int[]{1,2,3,4,5,6,7,8,9};
//2.利用tf对象调用方法执行操作
tf.changeArrayElements(x);//x--->array
//3.验证看一看结果
for(int v:x){
System.out.println(v);
}
}
}
3.设计一个方法 用来寻找数组中的极值(最大值 或 最小值)
public class TestFunctions {
/**
* 3.设计一个方法 用来寻找数组中的极值(最大值 或 最小值)
* 分析 参数 一个数组 boolean flag true 最大 false最小
* 返回值 找到的极值
*/
public int findMaxOrMinNum(int[] array,boolean flag){
//1.找一个变量
int temp=array[0];
//2.利用遍历数组的方式挨个与max比较
for (int i = 0; i <array.length ; i++) {
if (flag && temp< array[i]){ //找寻最大值
temp=array[i];
}else if (!flag && temp > array[i]){ //找寻最小值
temp=array[i];
}
}
return temp;
}
public static void main(String[] args) {
TestFunctions tf = new TestFunctions();
//1.有一个数组
int[] x = new int[]{4,5,11,2,3,};
//2.利用tf对象调用方法执行操作
int resule= tf.findMaxOrMinNum(x,true);
//3.验证看一看结果
System.out.println(resule);
}
}
4.设计一个方法 用来找寻给定的元素是否在数组内存在(Scanner输入一个)
public class TestFunctions {
/**
* 4.设计一个方法 用来找寻给定的元素是否在数组内存在(Scanner输入一个)
* 分析 参数 需要一个数组int[] 要找的数 int a
* 返回值 告诉结果 是否存在
*/
public String isExist(int[] array,int a){
String temp="你要找的元素不存在";
//循环对比
for (int i = 0; i <array.length ; i++) {
if (a==array[i] ){ //对比 找到后退出
temp="你要找的元素存在";
break;
}
}
return temp;
}
public static void main(String[] args) {
TestFunctions tf = new TestFunctions();
//1.有一个数组
int[] x = new int[]{4, 5, 11, 2, 3,};
//2.利用tf对象调用方法执行操作
String str =tf.isExist(x,5);
//3.验证看一看结果
System.out.println(str);
}
}
5.设计一个方法 用来合并两个数组
public class TestFunctions {
/**
* 5.设计一个方法 用来合并两个数组
* 分析 参数 需要两个数组int[]a int[]b
* 返回值 一个数组(合并后的)
*/
public int[] mergeArray(int[]a,int [] b){
//1.创建一个数组 长度为两个数组长度的和
int [] newArray =new int[a.length+b.length];
//2循环将a数组的元素放入 新数组newArray 里面
for (int i = 0; i <a.length ; i++) {
newArray[i]=a[i];
}
//3.循环将a数组的元素放入 新数组newArray 里面
for (int i = 0; i <b.length ; i++) {
//放入第二个元素时需要从a元素长度后面加
newArray[a.length+i]=b[i];
}
return newArray;
}
public static void main(String[] args) {
//交换数组内部元素
TestFunctions tf = new TestFunctions();
//1.有一个数组
int[] x = new int[]{4, 5, 11, 2, 3};
int[] y = new int[]{7,98,45,6};
//2.利用tf对象调用方法执行操作
int[] result=tf.mergeArray(x,y);
//3.验证看一看结果
for (int i : result) {
System.out.println(i);
}
}
}
6.设计一个方法 用来将一个数组按照最大值位置拆分
public class TestFunctions {
/**
* 6.设计一个方法 用来将一个数组按照最大值位置拆分
* 分析 参数 需要个数组int[]a
* 返回值 一个二维数组 里面有两个数组
*/
public int[][] splitArray(int[] array) {
//1.找出数组的最大值的索引位置
int max = array[0]; //记录最大数
int index = 0; //记录最大数的索引
for (int i = 0; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
index = i;
}
}
//根据索引创建两个新的数组
int[] newa = new int[index];
int[] newb = new int[array.length - index - 1];
//循环大数组并将元素加入到新数组
//分别将两个小数组填满
for(int i=0;i<newa.length;i++){
newa[i] = array[i];
}
for(int i=0;i<newb.length;i++){
newb[i] = array[(index+1)+i];
}
//创建二维数组 并返回
return new int[][]{newa, newb};
}
public static void main(String[] args) {
TestFunctions tf = new TestFunctions();
//1.有一个数组
int[] x = new int[]{12, 5, 11, 2, 18};
//2.利用tf对象调用方法执行操作
int[][] result = tf.splitArray(x);
//3.验证看一看结果
for (int i: result[0]) {
System.out.println("第一个数组 "+i);
}
for (int i: result[1]) {
System.out.println("第二个数组 "+i);
}
}
}
7.设计一个方法 用来去掉数组中的0元素
`public class TestFunctions {
/**
* 7.设计一个方法 用来去掉数组中的0元素
* 分析 参数 需要一个数组int[]a 需要去掉的元素
* 返回值 去掉元素后的新数组
*/
public int[] removeElementFromArray(int[] array,int element){
//1.查询出数组中有多少个要去掉的元素
int count=0; //记录要删除的元素的数量
for (int i = 0; i <array.length ; i++) {
if (array[i]==element){
count++;
}
}
//创建一个新数组
int[] newArray=new int[array.length-count];
int index=0 ;////控制新数组的索引变化
//将原来数组中非删除的元素存入新数组中
for (int i = 0; i <array.length ; i++) {
if (array[i]!=element){
newArray[index]=array[i];
index++;
}
}
return newArray;
}
public static void main(String[] args) {
TestFunctions tf = new TestFunctions();
//1.有一个数组
int[] x = new int[]{12, 5, 10,2,6,45,11, 2, 18};
//2.利用tf对象调用方法执行操作
int[] result = tf.removeElementFromArray(x,2);
//3.验证看一看结果
for (int i: result) {
System.out.println(i);
}
}
}
8.设计一个方法 用来存储给定范围内的素数(2-100) 素数在自然数之内
public class TestFunctions {
/**
* 8.设计一个方法 用来存储给定范围内的素数(2-100) 素数在自然数之内
* 分析 参数 范围: 开始位置 结束位置
* 返回值 给定范围内素数 的 数组
*/
public int[] findPrimeNum(int begin ,int end){
if(begin<0 || end<0){
System.out.println("素数没有负数 不给你找啦");
return null;//自定义一个异常 认为规定的一种不正常的现象
}
if(begin>=end){
System.out.println("您提供的范围有误 begin应该比end要小");
return null;//自定义一个异常 认为规定的一种不正常的现象
}
//创建一个足够长的数组
int[] array = new int[end/2];
int index = 0;//记录新数组的索引变化 同时记录个数
for(int num=begin;num<=end;num++){
boolean b = false;//标记
for(int i=2;i<=num/2;i++){
if(num%i==0){
b = true;
break;
}
}
if(!b){
array[index++] = num;
}
}
//将数组后面的多余的0去掉
int[] primeArray = new int[index];
for(int i=0;i<primeArray.length;i++){
primeArray[i] = array[i];
}
array = null;
return primeArray;
}
public static void main(String[] args) {
TestFunctions tf = new TestFunctions();
//1.利用tf对象调用方法执行操作
int[] result = tf.findPrimeNum(2,4);
//2.验证看一看结果
for (int i: result) {
System.out.println(i);
}
}
}
9.设计一个方法 用来给数组元素排序(冒泡排序算法) 既能升序又能降序
public class TestFunctions {
/**
* 9.设计一个方法 用来给数组元素排序(冒泡排序算法) 既能升序又能降序
* 分析 参数 一个数组 boolean true 升序 false 降序
* 返回值
*/
public void orderArray(int[]array ,boolean flag){
//冒泡排序
//外部循环控制
for (int i = 0; i <array.length ; i++) { //控制执行的轮次---数组的长度
//取出每个数对比 升序最大放到最后
//控制比较次数 数组倒数第二个array.length-1 和最后一个比较后就可以结束
for (int j = 0; j <array.length-1-i ; j++) {
if (flag &&array[j]>array[j+1] || !flag && array[j]<array[j+1]){
int temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}
public static void main(String[] args) {
TestFunctions tf=new TestFunctions();
int[] array={1,3,6,11,7,4,17};
tf.orderArray(array,false);
for (int i : array) {
System.out.println(i);
}
}
}
10.设计一个方法 用来实现用户登录认证(二维数组当作小数据库)
public class TestFunctions {
/**
* 10.设计一个方法 用来实现用户登录认证(二维数组当作小数据库)
* 分析 参数 帐号 密码
* 返回值 登录结果 是否登录成功
*/
//1.需要有小数据库---存储用户真实的账号和密码
private String[][] userBox = {{"郑中拓", "123456"}, {"渡一教育", "666666"}, {"Java", "888"}};
public String login(String userName, String password) {
//参数记录对比结果
String result = "帐号或密码错误";
//1.判断帐号是否正确
for (int i = 0; i < userBox.length; i++) {
if (userBox[i][0].equals(userName) ) {
if (userBox[i][1].equals(password)){
result = "帐号密码正确,登录成功";
break; //登录成功 结束循环对比
}
}
}
return result;
}
public static void main(String[] args) {
TestFunctions tf = new TestFunctions();
String str = tf.login("郑中", "123456");
System.out.println(str);
}
}