方法的設計
- 設計一個方法 用來畫星星(控制檯輸出) 只輸出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);
}
}