我感觉java离我越来越远了,呜呜呜呜,刚开始对String类 与StringBuffer类(据说StringBuilder更好),对多种类之间的方法糅合在一起应用,这很重要,下面以一道题为例子(c,java均有):
几个基本方法:
1.char charAt(int index)返回字符串中的字符;
2.StringBuf reverse() 字符串反转 如:123asd —dsa321
例题:
c语言代码(定义变量较多,便于理解):
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num = scanner.nextInt();
int t=num;
int digit=0;
int sum=0,m=0;
while(num!=0){
digit++;
m=num%10;
sum=sum*10+m;
num/=10;
}
num=t;
System.out.printf("%d\n",digit);
String s=String.valueOf(num);
int n=sum;
while(sum!=0){
m=sum%10;
if(m==sum/Math.pow(10,digit-1))
{
System.out.printf("%d",m);
break;
}
System.out.printf("%d ",m);
sum/=10;
}
System.out.printf("\n");
System.out.printf("%d\n",n);
}
}
java语言(方便)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
StringBuffer s2 = new StringBuffer();;
System.out.printf("%d\n",s.length());
for(int i=0;i<s.length();i++) {
s2.append(s.charAt(i));
if(i==s.length()-1) {
System.out.println(s.charAt(i));
break;
}
System.out.printf("%c ",s.charAt(i));
}
System.out.println(s2.reverse());
}
}