Java反序输出的一些方法

遇到一些题目,比如输入一个数将其反序输出,或者输入一个句子,将其反序输出类似的题目,这里总结了一下,献丑了。。。

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’”));

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值