第五天:數組、方法和字符串
- 一維數組
定義數組的語法:
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個學生三門課的成績,計算每個學生的平均分以及每門課程的平均分。
String[] stuNames = { "張三", "馮啓利", "李四", "馮奇", "風氣裏" };
String[] subNames = { "語文", "數學", "英語" };
int[][] scores = new int[stuNames.length][subNames.length];
int sum = 0;
int sum1 =0;
int sum2 =0;
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();
}
sum +=scores[i][0];
sum1 +=scores[i][1];
sum2 +=scores[i][2];
}System.out.println("語文:"+sum/5);
System.out.println("數學:"+sum1/5);
System.out.println("英語:"+sum2/5);
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();
}
}
}