练习1 输入字符串去除空格
import java.util.Scanner;
public class Stringtest2{
public static void main(String[] args){
System.out.println("请输入:");
Scanner in = new Scanner(System.in);
String a = in.nextLine();
System.out.println(a.replace(" ",""));
}
}
练习二 对比字符串
import java.util.Scanner;
public class ContrastString{
public static void main(String[] args){
System.out.println("请输入a:");
Scanner in = new Scanner(System.in);
String a = in.nextLine();
System.out.println("请输入b:");
String b = in.nextLine();
if (a.length() != b.length()){
System.out.println("不同");
return;
}
for(int i = 0; i < a.length(); i++){
if (a.charAt(i) != b.charAt(i)){
System.out.println("不同");
return;
}
}
System.out.println("相同");
}
}