实验一 Java编程环境使用

打印 上一主题 下一主题

主题 923|帖子 923|积分 2769

第1关 String类的常用方法
  1. package step1;
  2. public class StringExample {
  3.         public static void main(String args[]) {
  4.             /********* Begin *********/
  5.                 String s1 = new String("you are a student"),
  6.                            s2 = new String("how are you");
  7.                 if (s1.equals(s2)) // 使用equals方法判断s1与s2是否相同
  8.                 {
  9.                         System.out.println("s1与s2相同");
  10.                 } else {
  11.                         System.out.println("s1与s2不相同");
  12.                 }
  13.                 String s3 = new String("22030219851022024");
  14.                 if (s3.startsWith("220302")) // 判断s3的前缀是否是“220302”
  15.                 {
  16.                         System.out.println("吉林省的身份证");
  17.                 }
  18.                 String s4 = new String("你"), s5 = new String("我");
  19.                 if (s4.compareTo(s5) > 0)// 按着字典序s4大于s5的表达式
  20.                 {
  21.                         System.out.println("按字典序s4大于s5");
  22.                 } else {
  23.                         System.out.println("按字典序s4小于s5");
  24.                 }
  25.                 int position = 0;
  26.                 String path = "c:\\java\\jsp\\A.java";
  27.                 position = path.lastIndexOf("\"); // 获取path中最后出现\\的位置
  28.                 System.out.println("c:\\java\\jsp\\A.java中最后出现\\的位置:" + position);
  29.                 String fileName = path.substring(path.lastIndexOf("\") + 1); // 获取path中“A.java”子字符串
  30.                 System.out.println("c:\\java\\jsp\\A.java中含有的文件名:" + fileName);
  31.                 String s6 = new String("100"), s7 = new String("123.678");
  32.                 int n1 = Integer.parseInt(s6); // 将s6转化成int型数据
  33.                 double n2 = Double.parseDouble(s7); // 将s7转化成double型数据
  34.                 double m = n1 + n2;
  35.                 System.out.println(m);
  36.                 String s8 = String.valueOf(m); // String调用valueOf(m)方法将m转化为字符串对象
  37.                 position = s8.indexOf(".");
  38.                 String temp = s8.substring(position + 1); // 获取s8中小数点后面的小数
  39.                 System.out.println("数字" + m + "有" + temp.length() + "位小数");
  40.                 String s9 = new String("ABCDEF");
  41.                 char a[] = s9.toCharArray();  // 将s9存放到数组a中
  42.                 for (int i = a.length - 1; i >= 0; i--) {
  43.                         System.out.print(" " + a[i]);
  44.                 }
  45.         /********* End *********/
  46.         }
  47. }
复制代码
第2关 矩阵转置
  1. package step2;
  2. import java.util.Scanner;
  3. public class SwapMatrix {
  4.         public static void main(String[] args) {
  5.                 Scanner scanner = new Scanner(System.in);
  6.                 int[][] matrix = new int[3][3];
  7.                 for (int i = 0; i < 3; i++) {
  8.                         for (int j = 0; j < 3; j++) {
  9.                                 matrix[i][j] = scanner.nextInt();
  10.                         }
  11.                 }
  12.         /********** Begin **********/
  13.                 System.out.println("原始数组为:");
  14.                 for (int i = 0; i < 3; i++) {
  15.                         for (int j = 0; j < 3; j++) {
  16.                                 System.out.print(matrix[i][j] + " ");
  17.                         }
  18.                         System.out.println();
  19.                 }
  20.                
  21.                 int[][] transposedMatrix = new int[3][3];
  22.                 for (int i = 0; i < 3; i++) {
  23.                         for (int j = 0; j < 3; j++) {
  24.                                 transposedMatrix[i][j] = matrix[j][i];
  25.                         }
  26.                 }
  27.                
  28.                 System.out.println("行列互调后数组为:");
  29.                 for (int i = 0; i < 3; i++) {
  30.                         for (int j = 0; j < 3; j++) {
  31.                                 System.out.print(transposedMatrix[i][j] + " ");
  32.                         }
  33.                         System.out.println();
  34.                 }
  35.         /********** End **********/
  36.                 scanner.close();
  37.         }
  38. }
复制代码
第3关 求平均分及各个区间段的人数
  1. package step3;
  2. import java.util.Scanner;
  3. public class Score {
  4.         public static void main(String args[]) {
  5.                 Scanner scanner = new Scanner(System.in);
  6.                 /********** Begin **********/
  7.         int excellent = 0; // 优秀人数
  8.         int good = 0; // 良好人数
  9.         int medium = 0; // 中等人数
  10.         int pass = 0; // 及格人数
  11.         int fail = 0; // 不及格人数
  12.         double sum = 0; // 总成绩
  13.         double score; // 学生成绩
  14.         int count = 0; // 学生总人数
  15.         while (true) {
  16.             score = scanner.nextDouble();
  17.             if (score == -1) {
  18.                 break;
  19.             }
  20.             count++;
  21.             sum += score;
  22.             if (score >= 90) {
  23.                 excellent++;
  24.             } else if (score >= 80) {
  25.                 good++;
  26.             } else if (score >= 70) {
  27.                 medium++;
  28.             } else if (score >= 60) {
  29.                 pass++;
  30.             } else {
  31.                 fail++;
  32.             }
  33.         }
  34.         System.out.println("不及格的人数为:" + fail);
  35.         System.out.println("及格的人数为:" + pass);
  36.         System.out.println("中等的人数为:" + medium);
  37.         System.out.println("良好的人数为:" + good);
  38.         System.out.println("优秀的人数为:" + excellent);
  39.         System.out.printf("全班平均分为:%.1f\n", sum / count);
  40.         /********** End **********/
  41.                 scanner.close();
  42.         }
  43. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

河曲智叟

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表