第五周课程总结&试验报告(三)

实验三 String类的应用Java实验报告

班级:计科二班      学号 :20188435          姓名 :徐鸿敏         完成时间

评分等级

实验目的:

  • 掌握类String类的使用;
  • 学会使用JDK帮助文档;
  • 实验内容

1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)

  • 统计该字符串中字母s出现的次数。
  • 统计该字符串中子串“is”出现的次数。
  • 统计该字符串中单词“is”出现的次数。
  • 实现该字符串的倒序输出。

2.请编写一个程序,使用下述算法加密或解密用户输入的英文字串。要求源代码、结果截图。

 

 3.已知字符串“ddejidsEFALDFfnef2357 3ed”。输出字符串里的大写字母数,小写英文字母数,非英文字母数。

String的主要方法:

1.Length():获取当前字串长度
2.charAt(int index):获取当前字符串对象下标index处的字符
3.getChars():获取从指定位置起的子串复制到字符数组中
4.replace(char ch1,char ch2):将字符串的字符ch1替换为字符串ch2.
5.toUpperCase():将字符串中的小写字符转换为大写字符
6.toLowerCase():将字符串中的大写字符转换为小写字符
7.toCharArray():将字符串对象转换为字符数组

Java基本方法命名:

1.方法命名为【修饰符1,2,3】 返回值类型 方法名 (形式参数);并且是有return作为结束
2.无返回值的时候 必须要用指定为void
3.实参的数量,数值,及数据类型和次序,必须和形参的一致;
4.java进行方法调用的时候,数据传递是值传递,传递的都是数据的副本
5.基本数据类型传递的是值的copy值
6.引用数据类型传递的是对象引用的copy值,所指的是同一个对象

开头出处:https://www.cnblogs.com/fengmixinluo/p/11586934.html

1.(1)实验代码:

package com.company;
class
Test { public static void main(String[] args){ int n=0,m=0,x=0; String str="this is a test of java"; char[] c=str.toCharArray(); for(int i=0;i<c.length;i++){ if(c[i]=='s'){ n++; } if(c[i]=='i'&&c[i+1]=='s'){ m++; if(c[i-1]==' '&&c[i+2]==' '){ x++; } } } System.out.println("字符串中字母s出现的次数:"+n); System.out.println("字符串中字串“is”出现的次数:"+m); System.out.println("字符串中单词“is”出现的次数:"+x); System.out.print("字符串的倒序输出:"); for(int i=c.length-1;i>=0;i--){ System.out.print(c[i]); } } }

(2)实验截图:

(3)运行结果:

(4)难点:

string方法的应用,记住就好

2.(1)实验代码:

package com.company;
import java.util.Scanner;
class Test11 {
    public static void main(String[] args){
        Scanner n=new Scanner(System.in);
        String ch=n.next();
        char[] c=ch.toCharArray();
        for(int i=0;i<c.length;i++){
            c[i]+=3;
        }
        System.out.print("加密后的结果:");
        for(int i=0;i<c.length;i++){
            System.out.print(c[i]);
        }
    }
}

(2)实验截图:

(3)运行结果:


(4)难点:

 

3.(1)实验代码:

package com.company;

public class Text {
    public static void main(String[] args){
        int count1=0,count2=0,count3=0;
        String ch="ddejidsEFALDFfnef2357 3ed";
        char[] c=ch.toCharArray();
        for(int i=0;i<c.length;i++){
            if(c[i]>='A'&&c[i]<='Z'){
                count1++;
            }
            else if(c[i]>='a'&&c[i]<='z'){
                count2++;
            }
            else {
                count3++;
            }
        }
        System.out.println("大写字母数:"+count1);
        System.out.println("小写字母数:"+count2);
        System.out.println("非英文字母数:"+count3);
    }
}

(2)实验截图:

(3)运行结果:


(4)难点:

没想太多,就用c算了

总结:


本周作业比之前的简单,知识点掌握到了就可以了

原文地址:https://www.cnblogs.com/girlsteam/p/11597719.html