欢迎来到CloudService文涵的博客

生三从境界:昨夜西风凋碧树,独上高楼,望尽天涯路。 衣带渐宽终不悔,为伊消得人憔悴。 众里寻他千百度,蓦然回首,那人却在灯火阑珊处。人
随笔 - 127  文章 - 0  评论 - 11  阅读 - 15万

Java实验一:编程环境使用(头歌)

1.String类的常用方法(StringExample.java)

阅读分析下面程序,把程序中的代码补充完整,并查看结果。

package step1;

public class StringExample {
	public static void main(String args[]) {
		String s1 = new String("you are a student");
		String s2 = new String("how are you");
		// 使用equals方法判断s1与s2是否相同
		if ( (s1.equals(s2)==true )) {
			System.out.println("s1与s2相同");
		} else {
			System.out.println("s1与s2不相同");
		}
		String s3 = new String("13971918888");
		// 判断手机号是否以“139”开头
		if(s3.startsWith("139")==true ) {
			System.out.println("手机号以139开头");
		}
		String s4 = new String("你"), s5 = new String("我");
		// 按着字典序s4大于s5的表达式
		if ( s4.compareTo(s5)>0 ) {
			System.out.println("按字典序s4大于s5");
		} else {
			System.out.println("按字典序s4小于s5");
		}
		/******************************************************/
		int position = 0;
		String path = "d:\\java\\A.java";
		// 获取path中最后出现\\的位置
		position =  path.lastIndexOf("\\") ;
		System.out.println(path + "中最后出现\\的位置为: " + position);
		// 利用字符串截取方法获取path中“A.java”子字符串
		String fileName =  path.substring(8, 14);
		System.out.println(path + "中含有的文件名为: " + fileName);
		/******************************************************/
		String s6 = new String("100");
		String s7 = new String("123.678");
		// 将s6转化成int型数据
		int n1 =  Integer.parseInt(s6) ;
		// 将s7转化成double型数据
		double n2 =   Double.parseDouble(s7) ;
		double m = n1 + n2;
		System.out.println(n1 + " + " + n2 + " 的和为: " + m);
		// String类调用valuOf(double n)方法将m转化为字符串对象
		String s8 =  String.valueOf(m);
		position = s8.indexOf(".");
		// 利用字符串截取方法获取s8中小数点后面的小数
		String temp =  s8.substring((s8.indexOf("."))+1);
		System.out.println("数字" + m + "有" + temp.length() + "位小数");
		String s9 = new String("ABCDEF");
		// 将s9存放到数组a中
		char a[] = s9.toCharArray() ;
		System.out.print(s9 + " 逆序字符输出为: ");
		for (int i = a.length - 1; i >= 0; i--) {
			System.out.print(" " + a[i]);
		}
	}
}

2.编写Java程序(SwapMatrix.java)

要求:定义一个二维数组,并给二维数组赋值,分别打印原二维数组和行列互调后的二维数组。
image

复制下面代码替换即可
image

package step2;
import java.util.Scanner;
public class SwapMatrix {
 
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		int array[][] = new int[3][3];
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++)
				array[i][j] = input.nextInt();
		}
        System.out.println("原始数组为:");
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 3; j++)
				System.out.print(array[i][j]+" ");
			System.out.println();
		}
        System.out.println("行列互调后数组为:");
		for (int j = 0;j<3;j++) {
			for(int i = 0;i<3;i++)
				System.out.print(array[i][j]+" " );
			System.out.println();
		}
	}
}

3.统计一批学生的成绩,求平均分及各个区间段的人数(Score.java)

要求:输入一批学生成绩,以-1作为结束标记。

统计这批学生中不及格(<60)、及格(60~69)、中(70~79)、良(80~89)、优(>90)的人数。 求这批学生的平均分。

提示:这是一个计数和累加问题。学生数量不确定,但有一个结束标记(-1),该问题的总体结构是一个循环处理问题,可用while循环,当输入数据为-1时结束循环。为了统计各种情况的人数,需要设立相应的计数变量,并给其赋初值0,另外为了求平均分,必须计算总分,也就是计算出所有学生成绩的累加和,然后除以总人数即可得到平均分。

输入数据样例:75.5 80 92.5 64.5 55 87.5 98 -1

输出结果样例:

不及格的人数为:1

及格的人数为:1

中等的人数为:1

良好的人数为:2

优秀的人数为:2

全班平均分为:79.0

package step3;
import java.util.Scanner;
 
public class Score {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
 
        int failCount = 0;
        int passCount = 0;
        int middleCount = 0;
        int goodCount = 0;
        int excellentCount = 0;
 
        double totalScore = 0;
        int studentCount = 0;
 
        
 
        while (true) {
            double score = input.nextDouble();
 
            if (score == -1) {
                break;
            }
            if (score < 0 || score > 100) {
            	System.out.println("请输入0~100之间的数。");
            }
            totalScore += score;
            studentCount++;
 
            if (score < 60) {
                failCount++;
            } else if (score < 70) {
                passCount++;
            } else if (score < 80) {
                middleCount++;
            } else if (score < 90) {
                goodCount++;
            } else {
                excellentCount++;
            }
        }
		
        double averageScore = totalScore / studentCount;

        System.out.println("不及格的人数为:" + failCount);
        System.out.println("及格的人数为:" + passCount);
        System.out.println("中等的人数为:" + middleCount);
        System.out.println("良好的人数为:" + goodCount);
        System.out.println("优秀的人数为:" + excellentCount);
        System.out.printf("全班平均分为:%.1f",averageScore );
    }
}
posted on   Cloudservice  阅读(755)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示

目录导航