第五天:數組、方法和字符串
- 一維數組
定義數組的語法:
T[] x = new T[size];
T[] y = { value1, value2, ... };
- 操作數組元素可以使用下標運算[ ],數組的下標範圍0-(數組大小-1)。
- 數組有一個length屬性表示數組元素的個數。
- 通常可以用循環來對數組中的元素進行操作。
練習1:錄入5名學生的成績,計算平均分,找出最高分和最低分。
package com.lovoinfo;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] names = {"關羽", "張飛", "趙雲", "黃忠", "馬超"};
double[] scores = new double[5];
double sum = 0;
for(int i = 0; i < scores.length; i++) {
System.out.print("請輸入" + names[i] + "的成績: ");
scores[i] = sc.nextDouble();
sum = sum + scores[i];
}
System.out.println("平均分爲: " + sum / scores.length);
int maxIndex = 0, minIndex = 0;
for(int i = 1; i < scores.length; i++) {
if(scores[i] > scores[maxIndex]) {
maxIndex = i;
}
else if(scores[i] < scores[minIndex]) {
minIndex = i;
}
}
System.out.println(names[maxIndex] + "考了最高分" + scores[maxIndex]);
System.out.println(names[minIndex] + "考了最低分" + scores[minIndex]);
sc.close();
}
}
練習2:輸出前20個Fibonacci數。
1, 1, 2, 3, 5, 8, 13, 21, 34, …
package com.lovoinfo;
/**
* 輸出前20個Fibonacci數
* @author jackfrued
*
*/
public class Test04 {
public static void main(String[] args) {
int[] f = new int[20];
f[0] = f[1] = 1;
for(int i = 2; i < f.length; i++) {
f[i] = f[i - 1] + f[i - 2];
}
for(int x : f) {
System.out.println(x);
}
}
}
練習3:隨機產生10個數,並對其進行排序。
package com.lovoinfo;
public class Test05 {
public static void main(String[] args) {
int[] a = new int[10];
System.out.println("排序前: ");
for (int i = 0; i < a.length; i++) {
a[i] = (int) (Math.random() * 100);
System.out.print(a[i] + "\t");
}
bubbleSort(a);
System.out.println("\n排序後: ");
for (int x : a) {
System.out.print(x + "\t");
}
}
/**
* 冒泡排序
* @param a 待排序的數組
*/
public static void bubbleSort(int[] a) {
// N個元素排序需要N-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;
}
}
}
}
/**
* 簡單選擇排序
* @param a 待排序的數組
*/
public static void selectionSort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
int minIndex = i; // 假設當前的i是最小元素所在的位置
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[minIndex]) { // 發現更小的元素
minIndex = j; // 記錄更小的元素所在的位置
}
}
// 將i位置上的元素和最小元素交換位置
int temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
}
}
}
- 二維數組
練習4:輸入5個學生三門課的成績,計算每個學生的平均分以及每門課程的平均分。
package com.lovoinfo;
import java.util.Scanner;
public class Test07 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] stuNames = {"關羽", "張飛", "趙雲", "黃忠", "馬超"};
String[] subNames = {"語文", "數學", "英語"};
int[][] scores = new int[stuNames.length][subNames.length];
for(int i = 0; i < scores.length; i++) {
System.out.println("請錄入" + stuNames[i] + "的成績");
for(int j = 0; j < scores[i].length; j++) {
System.out.print("\t" + subNames[j] + ": ");
scores[i][j] = sc.nextInt();
}
}
// 後面的代碼自行完成
sc.close();
}
}
練習5:輸出10行楊輝三角。
package com.lovoinfo;
/**
* 楊輝三角
* @author jackfrued
*
*/
public class Test08 {
public static void main(String[] args) {
int[][] y = new int[10][];
for(int i = 0; i < y.length; i++) {
y[i] = new int[i + 1];
for(int j = 0; j < y[i].length; j++) {
if(j == 0 || j == i) {
y[i][j] = 1;
}
else {
y[i][j] = y[i - 1][j] + y[i - 1][j - 1];
}
}
}
for(int[] a : y) {
for(int b : a) {
System.out.print(b + "\t");
}
System.out.println();
}
}
}
練習六:用數組求平均值
package day10150428;
import java.util.Scanner;
/**
* 利用二維數組來做學生的平均成績,科目成績。
*
* @author 谷振東
*
*/
public class Test06 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] stuNames = { "呂布", "趙雲", "諸葛亮", "司馬懿", "周瑜" };
String[] subNames = { "語文", "體育", "科學" };
int[][] scores = new int[stuNames.length][subNames.length];
for (int i = 0; i < scores.length; i++) {
double sum = 0;//求各個學生成績總和。
System.out.println("請錄入"+ stuNames[i] + "成績");
for (int j = 0; j < scores[i].length; j++) {
System.out.print("\t" + subNames[j] + ":" );
scores[i][j] = sc.nextInt();
sum += scores[i][j];//sum = sum + scores[i][j];
//作業:統計出每個學生的平均成績,和學生科目平均成績。
}
System.out.println(stuNames[i]+"平均成績:"+ sum/3+"分");
}
System.out.println();
}
}
- 方法的定義和使用
- 方法的遞歸調用
- 字符串用法及常用方法