###第1關 String類的常用方法 package step1; public class StringExample { public static void main(String args[]) { /********* Begin *********/ String s1 = new S ...
第1關 String類的常用方法
package step1;
public class StringExample {
public static void main(String args[]) {
/********* Begin *********/
String s1 = new String("you are a student"),
s2 = new String("how are you");
if (s1.equals(s2)) // 使用equals方法判斷s1與s2是否相同
{
System.out.println("s1與s2相同");
} else {
System.out.println("s1與s2不相同");
}
String s3 = new String("22030219851022024");
if (s3.startsWith("220302")) // 判斷s3的首碼是否是“220302”
{
System.out.println("吉林省的身份證");
}
String s4 = new String("你"), s5 = new String("我");
if (s4.compareTo(s5) > 0)// 按著字典序s4大於s5的表達式
{
System.out.println("按字典序s4大於s5");
} else {
System.out.println("按字典序s4小於s5");
}
int position = 0;
String path = "c:\\java\\jsp\\A.java";
position = path.lastIndexOf("\\"); // 獲取path中最後出現\\的位置
System.out.println("c:\\java\\jsp\\A.java中最後出現\\的位置:" + position);
String fileName = path.substring(path.lastIndexOf("\\") + 1); // 獲取path中“A.java”子字元串
System.out.println("c:\\java\\jsp\\A.java中含有的文件名:" + fileName);
String s6 = new String("100"), s7 = new String("123.678");
int n1 = Integer.parseInt(s6); // 將s6轉化成int型數據
double n2 = Double.parseDouble(s7); // 將s7轉化成double型數據
double m = n1 + n2;
System.out.println(m);
String s8 = String.valueOf(m); // String調用valueOf(m)方法將m轉化為字元串對象
position = s8.indexOf(".");
String temp = s8.substring(position + 1); // 獲取s8中小數點後面的小數
System.out.println("數字" + m + "有" + temp.length() + "位小數");
String s9 = new String("ABCDEF");
char a[] = s9.toCharArray(); // 將s9存放到數組a中
for (int i = a.length - 1; i >= 0; i--) {
System.out.print(" " + a[i]);
}
/********* End *********/
}
}
第2關 矩陣轉置
package step2;
import java.util.Scanner;
public class SwapMatrix {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[][] matrix = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
matrix[i][j] = scanner.nextInt();
}
}
/********** Begin **********/
System.out.println("原始數組為:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
int[][] transposedMatrix = new int[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
transposedMatrix[i][j] = matrix[j][i];
}
}
System.out.println("行列互調後數組為:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(transposedMatrix[i][j] + " ");
}
System.out.println();
}
/********** End **********/
scanner.close();
}
}
第3關 求平均分及各個區間段的人數
package step3;
import java.util.Scanner;
public class Score {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
/********** Begin **********/
int excellent = 0; // 優秀人數
int good = 0; // 良好人數
int medium = 0; // 中等人數
int pass = 0; // 及格人數
int fail = 0; // 不及格人數
double sum = 0; // 總成績
double score; // 學生成績
int count = 0; // 學生總人數
while (true) {
score = scanner.nextDouble();
if (score == -1) {
break;
}
count++;
sum += score;
if (score >= 90) {
excellent++;
} else if (score >= 80) {
good++;
} else if (score >= 70) {
medium++;
} else if (score >= 60) {
pass++;
} else {
fail++;
}
}
System.out.println("不及格的人數為:" + fail);
System.out.println("及格的人數為:" + pass);
System.out.println("中等的人數為:" + medium);
System.out.println("良好的人數為:" + good);
System.out.println("優秀的人數為:" + excellent);
System.out.printf("全班平均分為:%.1f\n", sum / count);
/********** End **********/
scanner.close();
}
}