Educoder -Java面向对象 - String类各关题目答案

第1关:length()方法与compareTo()方法的使用 - 花名册

package step1;

import java.util.Scanner;

public class Roster {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        /********** Begin *********/
        String roster1 = new String(scanner.nextLine());
        String roster2 = new String(scanner.nextLine());
        System.out.println(roster1.length());
        System.out.println(roster2.length());
        if(roster1.compareTo(roster2)==0){
            System.out.println("相同");
        }else{
            System.out.println("不相同");
        }


        /********** End *********/
    }
}

第2关:substring()方法与indexOf()方法的使用 - 姓名查找

package step2;

import java.util.Scanner;

public class NameSearch {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        /********** Begin *********/
        int i,j,r;
        int count=-2;
        String date = scanner.next();
        String name = scanner.next();
        for(i=0,j=0,r=2;(i!=-1)&&(i+r!=j);){
        j = date.length();
        r = name.length();
        i = date.indexOf(name);
        count = i+count+r;
        System.out.println(count);
        date=date.substring(i+r,j);
        }
        /********** End *********/
    }
}

第3关:String串类操作练习 - 文件名与邮箱验证

package step3;
import java.util.Scanner;
public class HelloWorld {
    
	public void judge(String fileName,String email){
		//请在此添加实现代码
/********** Begin **********/
    String last1="1";
    String last2="2";
    String last3="3";
    String last4="4";
    int i1 = fileName.length();
    if(i1<=5){
        System.out.println("Java文件名无效");   
    }else{
    last1 = ".java";
    last2 = fileName.substring(i1-5,i1);
    }
    if(last1.equals(last2)){
        System.out.println("Java文件名正确");
    }else if(i1>5) {
        System.out.println("Java文件名无效");
    }
    int o1=email.lastIndexOf("@");
    int o2=email.lastIndexOf(".");
    int i2=email.length();
    if(i2<=4){
        System.out.println("邮箱名无效");
    }else{
    last3=".com";
    last4=email.substring(i2-4,i2);
    }
    if((o1<o2)&&last3.equals(last4)){
        System.out.println("邮箱名正确");
    }else if(i2>4){
        System.out.println("邮箱名无效");
    }
/********** End **********/
    }
	
	
}

第4关:StringBuffer类的定义和使用 - 字母反转

package step4;

public class Reverse {
public static StringBuffer start(String data) {
StringBuffer ans = new StringBuffer();
/********** Begin *********/

    String [] words = data.split(" ");
    for(int i=0;i

}

你可能感兴趣的:(Educoder -Java面向对象 - String类各关题目答案)