遇到一些题目,比如输入一个数将其反序输出,或者输入一个句子,将其反序输出类似的题目,这里总结了一下,献丑了。。。
import java.util.Scanner;
public class Change {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
/**
* 输入多位数,反转,
* 如123456789输出的是
* 987654321
*/
// long n=sc.nextLong();
// int i;
// while(n>0){
// i=(int)n%10;
// n=(n-i)/10;
// System.out.print(""+i);
// }
//
/**
* 以字符串来处理反转
* toCharArray
*/
// String s=sc.nextLine();
// char[] chars=s.toCharArray();
// for(int j=0;j<chars.length;j++){
// System.out.print(chars[chars.length-1-j]);
// }
//
/**
* 方法一:
* 句子反转,单词不反转
* 如this is basketball house
* 则为house basketball is this
*/
String s=sc.nextLine();
String array[]=s.split(" ");
if(array!=null&&array.length>1){
for(int k=array.length-1;k>=0;k--){
System.out.print(array[k]+" ");
}
}
/**
* 方法二:
* 句子反转,单词不反转
* 如this is basketball house
* 则为house basketball is this
*
*/
//可能存在的问题:The type java.lang.CharSequence cannot be resolved。。。。
//原因是insert那一句jdk版本太高,1.8的不稳定,退回1.7和1.6就可以使用
// int pos=0;
// String s=sc.nextLine();
// StringBuilder sb=new StringBuilder();
// for(int i=0;i<s.length();i++){
// char c=s.charAt(i);
// if(c==' ')
// {
// pos=0;
// }
// sb.insert(pos, c);
// if(c!=' ')
// {
// pos++;
// }
// System.out.print(sb.toString());
// }
}
}
此外数组转字符串是去掉中括号的方式是:
Pattern.compile(“\b([\w\W])\b”) //数组转字符串去掉中括号
.matcher(submitDiagnosisData.getList_symptom().toString().toString()
.substring(1, submitDiagnosisData.getList_symptom().toString().toString().length()-1)).replaceAll(“’$1’”));