#字符串分隔#需要补0时采用print方式来循环0,慎用!

字符串分隔

https://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str = in.nextLine();
        //判断是否能整除
        if(str.length() % 8 != 0){
            //不能整除则直接在后边打印0而没有使用字符串拼接的方法
		  	//此方法比较投机取巧,实际程序运行中还是尽量使用StringBuffer等可变字符串拼接的方法提高效率
		  	//因为System.out.print()方法相对来说更占用系统资源
            for(int i = 0 ; i < str.length() ; i += 8){
                if(i + 8 > str.length()){
                    System.out.print(str.substring(i,str.length()));
                    for(int j = 0 ; j < 8 - str.length() % 8 ; j++){
                        System.out.print("0");
                    }
                }else{
                    System.out.println(str.substring(i,i + 8));
                }
                
            }
        }else{
            //可以整除则使用下列方法
             for(int i = 0 ; i < str.length() ; i += 8){
                System.out.println(str.substring(i,i + 8));
            }
        }
        
        
        
        
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务