实验一 Java编程环境使用

第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();
	}
}

热门相关:骑士归来   重生当学神,又又又考第一了!   法医王妃不好当!   重生当学神,又又又考第一了!   朕是红颜祸水