String操作中的java逻辑错误

时间:2016-05-10 08:34:12

标签: java

Scanner sc=new Scanner(System.in);
String A=sc.next();
String B=sc.next();
System.out.println(A.length()+B.length());       

int x=(new Character(A.charAt(0)).toString()).compareToIgnoreCase(new Character(B.charAt(0)).toString());        

if(x>0){System.out.println("Yes");}

else {System.out.println("No");}


String a=A.replace(new Character(A.charAt(0)).toString(),new Character(A.charAt(0)).toString().toUpperCase()); 

String b=B.replace(new Character(B.charAt(0)).toString(),new Character(B.charAt(0)).toString().toUpperCase()); 
System.out.println(a+" "+b);

当我输入aa& amp时,问题是什么? a作为两个输入A& B必要的输出未被撤销。打印的第3行必须是Aa A.但这打印为AA A.任何想法为什么?

1 个答案:

答案 0 :(得分:0)

replace(old,new)的{​​{1}}函数将String的所有出现次数替换为old,因此您获得AA A.

尝试使用new的{​​{1}}功能代替:

replaceFirst(old,new)