Java基础day6
Java基礎day6
- Java基礎day6
- debug和基礎練習
- 1 debug模式
- 1.1 什么是debug模式
- 1.2 debug模式操作
- 2 基礎練習
- 2.2.1 減肥計劃if版本
- 2.2.2 減肥計劃switch版本
- 2.3 逢七跳過
- 2.4不死神兔
- 2.5百錢白雞
- 2.6數組元素求和
- 2.7判斷兩個數組是否相同
- 2.8查找元素在數組中出現的索引位置
- 2.9數組元素反轉
- 2.10評委打分
Java基礎day6
debug和基礎練習
1 debug模式
1.1 什么是debug模式
定義:debug是供程序員使用的程序調試工具,它可以用于查看程序的執行流程,也可以用于追蹤程序執行過程來調試程序。
1.2 debug模式操作
1、設置斷點
2、刪除斷點
2 基礎練習
2.2.1 減肥計劃if版本
需求:輸入星期數,顯示今天的減肥活動 周一:跑步 周二:游泳 周三:慢走 周四:動感單車 周五:拳擊 周六:爬山 周日:好好吃一頓
代碼實現:
/* 思路:1:鍵盤錄入一個星期數,用一個變量接收2:對星期數進行判斷,這里用 if 語句實現3:在對應的語句控制中輸出對應的減肥活動 */ public class day6 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("請輸入一個星期數:");int week = sc.nextInt();//一個星期七天if (week < 1 || week > 7) {System.out.println("你輸入的星期數有誤");}else if(week==1){System.out.println("跑步");}else if(week==2){System.out.println("游泳");}else if(week==3){System.out.println("慢走");}else if(week==4){System.out.println("動感單車");}else if(week==5){System.out.println("拳擊");}else if(week==6){System.out.println("爬山");}else if(week==7){System.out.println("好好吃一頓");}} }2.2.2 減肥計劃switch版本
public class day6 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("請輸入一個星期數:");int week = sc.nextInt();//一個星期七天switch(week){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;case 6:System.out.println("爬山");break;case 7:System.out.println("好好吃一頓");break;default:System.out.println("輸入的星期數有誤");}} }2.3 逢七跳過
需求:1-100中逢到帶7或者7的倍數的數字就跳過
代碼實現:
2.4不死神兔
需求:有一對兔子,從出生后第3個月起每個月都生一對兔子,小兔子長到第三個月后每個月又生一對兔子, 假如兔子都不死,問第二十個月的兔子對數為多少?
代碼實現:
/* 思路:1:為了存儲多個月的兔子對數,定義一個數組,用動態初始化完成數組元素的初始化,長度為202:因為第1個月,第2個月兔子的對數是已知的,都是1,所以數組的第1個元素,第2個元素值也都是13:用循環實現計算每個月的兔子對數 4:輸出數組中最后一個元素的值,就是第20個月的兔子對數 */ public class day6 {public static void main(String[] args) {int[] arr = new int[20];arr[0] = 1;arr[1] = 1;for(int i = 2; i<arr.length; i++){arr[i] = arr[i-2]+arr[i-1];}System.out.println(arr[19]);} }2.5百錢白雞
需求:我國古代數學家張丘建在《算經》一書中提出的數學問題:雞翁一值錢五,雞母一值錢三,雞雛三值錢一。 百錢買百雞,問雞翁、雞母、雞雛各幾何?
代碼實現:
public class day6 {public static void main(String[] args) {for (int i = 0; i < 20; i++) {for (int j = 0; j < 33; j++) {int k = 100 - i - j;if (k % 3 == 0 && 5 * i + 3 * j + k / 3 == 100) {System.out.println(i + " " + j + " " + k);}}}} }2.6數組元素求和
需求:有這樣的一個數組,元素是{68,27,95,88,171,996,51,210}。求出該數組中滿足要求的元素和, 要求是:求和的元素個位和十位都不能是7,并且只能是偶數
代碼實現:
/* 思路:1:定義一個數組,用靜態初始化完成數組元素的初始化2:定義一個求和變量,初始值是03:遍歷數組,獲取到數組中的每一個元素4:判斷該元素是否滿足條件,如果滿足條件就累加5:輸出求和變量的值 */ public class day6 {public static void main(String[] args) {int[] arr = {68, 27, 95, 88, 171, 996, 51, 210};int sum = 0;for (int i = 0; arr[i]/10 != 7 && arr[i]/10%10 != 7 && arr[i]%7 != 0; i++) {sum += arr[i];}System.out.println(sum);} }2.7判斷兩個數組是否相同
需求:定義一個方法,用于比較兩個數組的內容是否相同
代碼實現:
/* 思路:1:定義兩個數組,分別使用靜態初始化完成數組元素的初始化2:定義一個方法,用于比較兩個數組的內容是否相同3:比較兩個數組的內容是否相同,按照下面的步驟實現就可以了首先比較數組長度,如果長度不相同,數組內容肯定不相同,返回false其次遍歷,比較兩個數組中的每一個元素,只要有元素不相同,返回false 最后循環遍歷結束后,返回true4:調用方法,用變量接收5:輸出結果 */ public class day6{public static void main(String[] args) {int[] arr = {11, 22, 33, 44, 55};int[] arr2 = {11, 22, 33, 44, 55};//調用方法boolean flag = compare(arr,arr2);System.out.println(flag);}//定義一個方法用于比較2個數組是否相同//兩個明確:返回值類型Boolean、參數int[] arr/arr2public static boolean compare(int[] arr,int[] arr2){if(arr.length != arr2.length){return false;}for(int i=0;i<arr.length;i++){if(arr[i] != arr2[i]){return false;}}return true;} }2.8查找元素在數組中出現的索引位置
需求:已知一個數組 arr = {19, 28, 37, 46, 50}; 鍵盤錄入一個數據,查找該數據在數組中的索引。并在控制臺輸出找到的索引值。如果沒有查找到,則輸出-1
代碼實現:
/* 思路:1:定義一個數組,用靜態初始化完成數組元素的初始化2:鍵盤錄入要查找的數據,用一個變量接收3:定義一個索引變量,初始值為-14:遍歷數組,獲取到數組中的每一個元素5:拿鍵盤錄入的數據和數組中的每一個元素進行比較,如果值相同,就把該值對應的索引賦值給索引變量,并 結束循環6:輸出索引變量 */ public class day6 {public static void main(String[] args) {int[] arr = {19, 28, 37, 46, 50};Scanner sc = new Scanner(System.in);System.out.println("請輸入要查找的數據");int number = sc.nextInt();//調用方法int index = getIndex(arr, number);//輸出索引變量System.out.println("輸出索引值" + index);}//定義方法 // 兩個明確:返回值類型為int、參數為arr和numberpublic static int getIndex(int arr[], int number) {int index = -1;//遍歷數組,獲取數組中的每一個元素for (int i = 1; i < arr.length; i++) {if (number == arr[i]) {index = i;break;}}return index;} }2.9數組元素反轉
需求:已知一個數組 arr = {19, 28, 37, 46, 50}; 用程序實現把數組中的元素值交換, 交換后的數組 arr = {50, 46, 37, 28,19}; 并在控制臺輸出交換后的數組元素。
代碼實現:
/* 思路:1:定義一個數組,用靜態初始化完成數組元素的初始化2:循環遍歷數組,這一次初始化語句定義兩個索引變量,判斷條件是開始索引小于等于結束索引3:變量交換4:遍歷數組 */ public class day6 {public static void main(String[] args) {//定義數組,靜態初始化int[] arr = {19, 28, 37, 46, 50};//調用方法reversereverse(arr);//遍歷數組printarrayprintArray(arr);}public static void reverse(int[] arr) {for (int i = 0, j = arr.length-1; i <= j; i++, j--) {int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}}public static void printArray(int[] arr) {System.out.print("[");for (int i = 0; i < arr.length; i++) {if (i == arr.length - 1) {System.out.print(arr[i]);} else {System.out.print(arr[i] + ", ");}}System.out.print("]");} }2.10評委打分
需求:
在編程競賽中,有6個評委為參賽的選手打分,分數為0-100的整數分。 選手的最后得分為:去掉一個最高分和一個最低分后 的4個評委平均值 (不考慮小數部分)。
代碼實現:
總結
以上是生活随笔為你收集整理的Java基础day6的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring的@ControllerAd
- 下一篇: 学习Spring Boot:(十八)Sp