swaggy郭钰轩

实验报告3&学习总结

1.已知字符串:"this is a test of java".按要求执行以下操作:
统计该字符串中字母s出现的次数。
统计该字符串中子串“is”出现的次数。
统计该字符串中单词“is”出现的次数。
实现该字符串的倒序输出。

package test2;

public class Test111 {
		 
		 String s = "this is a test of java";
		 static Test111 OneString = new Test111();
		 public static void main(String[] args) {
		  OneString.numberS();
		  OneString.numberIS();
		  OneString.numberwordIS();
		  OneString.reversal();
		 }
		 public void numberS() {
		  int number = 0;
		  for(int i = 0 ; i < s.length(); i++) {
		   char c = s.charAt(i);
		   if(c=='s') {
		    number++;
		   }
		  }
		  System.out.println("S出现次数"+number);
		 }
		 public void numberIS() {
		  int number = 0;
		  for(int i = 1 ; i < s.length() ; i++) {
		   char c = s.charAt(i-1);
		   char c1 = s.charAt(i);
		   if(c=='i'&&c1=='s') {
		    number++;
		   }
		  }
		  System.out.println("字符IS出现次数"+number);
		 }
		 public void numberwordIS() {
		  int number = 0;
		  String s[] = this.s.split(" ");
		  for(String s1 : s) {
		   if(s1.equalsIgnoreCase("is")) {
		    number++;
		   }
		  }
		  System.out.println("单词IS"+number);
		 }
		 public void reversal() {
		  StringBuilder word  = new StringBuilder();
		  for(int i = s.length()-1 ; i>=0 ; i--) {
		   char c = s.charAt(i);
		   word = word.append(s.charAt(i));
		  }
		  System.out.println("倒序输出 " + word);
		 }
		}

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

package test2;
public class simple {
		 static String s = "ddejjdsEFALDFfnef2357 3ed";
		 public static void main(String[] args) {
		  int Word = 0;
		  int word = 0;
		  int other = 0;
		  for(int i = 0;i < s.length();i++) {
		   char c = s.charAt(i);
		   if(c>='A' && c<='Z') {
		    Word++;
		   }else 
		   if(c>='a' &&c<='z') {
		    word++;
		   }else {
		    other++;
		   }
		  }
		  System.out.print("大写字母: "+Word+",小写字母 "+word+",其他 "+other);
	}
}

学习总结
这周学了一些关于 String类的用法
学习了一些关于继承的知识以及对于子类调用父类的方法与构造方法的注意事项
还了解了抽象类的一些知识

posted on 2019-09-27 14:16  gggyx  阅读(157)  评论(0编辑  收藏  举报

导航