第五周的作业

实验三 String类的应用
实验目的
掌握类String类的使用;
1,已经有一定的使用能力,但是还不是非常熟悉。
学会使用JDK帮助文档;
1,已经可以使用jdk来编程,jdk帮助文档还不是非常熟悉
实验内容
1.已知字符串:"this is a test of java".按要求执行以下操作:(要求源代码、结果截图。)
统计该字符串中字母s出现的次数。

package 上课时间;

public class 编程{

          public static void main(String[] args){
               int count=0;
               String str1="this is a test of java";
               char c[]=str1.toCharArray();
               for(int i=0;i<c.length;i++){
                     if(c[i]=='s'){
                           count++;
                     }
               }
               System.out.println(count);
         }
}


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

package 上课时间;

public class 编程{

          public static void main(String[] args){
               int count=0;
               String str1="this is a test of java";
               char c[]=str1.toCharArray();
               for(int i=0;i<c.length;i++){
                     if(c[i]=='i'&&c[i+1]=='s'){
                           count++;
                     }
               }
               System.out.println(count);
         }
}

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

package 上课时间;

public class 编程{

          public static void main(String[] args){
               int count=0;
               String str1="this is a test of java";
               char c[]=str1.toCharArray();
               for(int i=0;i<c.length;i++){
                     if(c[i]==' '&&c[i+1]=='i'&&c[i+2]=='s'&&c[i+3]==' '){
                           count++;
                     }
               }
               System.out.println(count);
         }
}

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

package 上课时间;

public class 编程{

          public static void main(String[] args){
               String str1="this is a test of java";
               char c[]=str1.toCharArray();
               for (int i=c.length-1; i>=0; i--){  
                   System.out.print(c[i]);  
               }
         }
}

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

package 上课时间;
import java.util.*; 
public class 编程{

          public static void main(String[] args){
        	  Scanner s = new Scanner(System.in);  
              System.out.print("请输入一行字符串");  
              String zf = s.nextLine();
              char c[]=zf.toCharArray();
              int n=0;
              for(int i=0;i<c.length;i++) {
            	  if(i<3) {
            		  n=c.length+i-3;
            	  }
            	  else {
            		  n=i-3;
            	  }
            	  System.out.print(c[n]);
              }
         }
}


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

package 上课时间;

public class 编程{

          public static void main(String[] args){
        	  int capital=0;
        	  int small=0;
        	  int wrong=0;
               String str1="ddejidsEFALDFfnef2357 3ed";
               char c[]=str1.toCharArray();
               for (int i=0; i<c.length; i++){ 
            	   if((c[i]<=90)&&(c[i]>=65)) {
            		   capital++;
            	   }
            	   else {
            		   if((c[i]<=122)&&(c[i]>=97)) {
            			   small++;
            		   }
            		   else{
            			   wrong++;
            			   }
                  }
              }
               System.out.println("大写字母数:"+capital+"小写英文字母数:"+small+"非英文字母数:"+wrong);
        }
}


总结
1、对于这次的作业,我大部分没有使用String类的使用方法,对于这次作业我有一些的东西还是不够了解。
2、对于第二题的加密,我有点没有理解意思,所以我是直接把输入的字符串直接输出,把后面三位转移到前面,然后再输出1到倒数第四位。
3,我的代码是非常的短的,也没有使用一些类方法,所以应用的地方可能不多。
4,对于java的学习,我还是有许多地方没有搞懂,就像上一次的课堂测试,我无法全部写对,可能跟我的一些学习方法有问题,上课注意力不太集中。

原文地址:https://www.cnblogs.com/he932206959/p/11598369.html