zoukankan      html  css  js  c++  java
  • 第五周总结&实验报告三

    第五周总结&实验报告三

    实验报告

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

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

    ① 统计该字符串中字母s出现的次数。

    package test3;
    
    public class Zifu {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String str = "this is a test of java";
    		char c[]=str.toCharArray();
    		char s='s';
    		int count=0;
    		for(int j=0;j<=c.length-1;j++)
    			if(s==c[j])
    				count++;
    			 
    		     
    	System.out.print("字符s出现的次数:"+count);
          }
    
    }
    

    ② 统计该字符串中子串“is”出现的次数。

    package test3;
    
    public class Zifu {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String str = "this is a test of java";
    		char c[]=str.toCharArray();
    		char s='s',i='i';
    		int count=0;
    		for(int j=0;j<=c.length-1;j++)
    			if(i==c[j]&&s==c[j+1])
    				count++;
    			 
    		     
    	System.out.print(“字串is出现的次数:"+count);
          }
    
    }
    

    ③ 统计该字符串中单词“is”出现的次数。

    package test3;
    
    public class Zifu {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String str = "this is a test of java";
    		char c[]=str.toCharArray();
    		char s='s',i='i';
    		int count=0;
    		for(int j=0;j<=c.length-1;j++)
    			if(c[j]==' '&&i==c[j+1]&&s==c[j+2]&&c[j+3]==' ')
    				count++;
    			 
    		     
    	System.out.print("单词is出现的次数:"+count);
          }
    
    }
    

    ④ 实现该字符串的倒序输出。

    package test3;
    
    public class Zifu {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String str = "this is a test of java";
    		String c[]=str.split(" ");
    		
    		for(int j=c.length-1;j>=0;j--)
    			
    			 System.out.print(c[j]+" ");
          }
    
    }
    

    这个题目其实比较简单,只要掌握了String方法就能做出来。

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

    package test4;
    
    import java.util.Scanner;
    public class Jiemi {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		char a,b,d;
    		Scanner scanner = new Scanner(System.in);
    		String str=scanner.next();
    		char e[]=str.toCharArray();
    		char c[]=str.toCharArray();
    		String result[]=str.split("");
    		for(int x=0;x<result.length;x++) {
    			System.out.print(result[x]+" ");
    			
    		}
    		System.out.println(" ");
    		a=c[c.length-1];
    		b=c[c.length-2];
    		d=c[c.length-3];
    		for(int i=0;i<c.length-3;i++) {
    			e[i+3]=c[i];
    			
    		}
    		e[0]=d;
    		e[1]=b;
    		e[2]=a;
    		for(int i=0;i<c.length;i++)
    			System.out.print(e[i]);
    		
    
    	}
    
    }
    

    这个题目的话我觉得主要考察对字符的运用,以及String类方法的运用,也是比较简单的。

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

    package test5;
    
    public class Zifutongji {
    
    	public static void main(String[] args) {
    		String str="ddejidsEFALDFfnef2357 3ed";
    		int m=0,n=0,k=0;
    		char[] c=str.toCharArray();
    		for(int i=0;i<str.length();i++)
    		{
    			if(c[i]>='a'&&c[i]<='z')
    			{
    				m++;
    			}
    			else if(c[i]>='A'&&c[i]<='Z')
    			{
    				n++;
    			}
    			
    			else {
    				k++;
    			}
    		}
    		System.out.println("小写字母出现的次数: "+m);
    		System.out.println("大写字母出现的次数: "+n);
    		System.out.println("其他字符出现的字数: "+k);
            
    	}
    
    }
    


    这个题目也是考察的字符串类问题,这是一个统计字符的题目,我们以前做过类似的,所以相对来说也没有很大的问题。

    课程总结

    这周我们学习了子类用extends继承父类的应用,并用super可以从子类中调用父类中的构造方法,
    并且学习了方法的覆写,在子类写一个与父类相同的构造方法把父类中的方法覆盖掉,并输出子类方法中的内容
    还有抽象类,抽象类必须用abstract声明,抽象类必须被子类继承,,含有抽象方法的类必须是抽象类等等。

  • 相关阅读:
    将同一个应用程序同时作为 http 和 https
    将数组元素划分为等长的块(二维数组)
    将数组中的空元素转为 undefined
    将某个类型断言为另一个与之毫无关系的类型
    将前端代码放入 Egg 项目中
    将根组件挂载到 DOM 节点上
    将类数组对象转换成数组
    将 ts 代码转成 js 代码
    将代码推迟到系统资源空闲时执行
    React 将 state 传给子组件用
  • 原文地址:https://www.cnblogs.com/djhxxx/p/11594697.html
Copyright © 2011-2022 走看看