按示例格式排版输出

编写代码,接受从屏幕输入的长度为16字节整数倍的字符串,回车后,按示例格式排版输出

示例:

屏幕输入(均为可见字符):

abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl

屏幕输出(斜体部分是格式说明,不要在结果里输出):

16进制偏移                       16进制表示(每一行16个字节)                      原文

       双空格分隔                       双空格分隔                       双空格分隔

00000000  61 62 63 64 65 66 67 68  69 6a 6b 6c 6d 6e 6f 70  abcdefghijklmnop

00000010  71 72 73 74 75 76 77 78  79 7a 61 62 63 64 65 66  qrstuvvwxyzabcdef

00000020  67 68 69 6a 6b 6c 6d 6e  6f 70 71 72 73 74 75 76  ghijklmnopqrstuv

00000030  77 78 79 7a 61 62 63 64  65 66 67 68 69 6a 6b 6c  wxyzabcdefghijkl

 

 1 import java.util.Scanner;
 2 
 3 public class Tencent_Main2 {
 4     public static void main(String[] args) {
 5         Scanner sc=new Scanner(System.in);
 6         while(sc.hasNext()){
 7             String s=sc.nextLine();
 8             int start=0;
 9             while(start!=s.length()){
10                 String res=Integer.toHexString(start);
11                 int len=res.length();
12                 for(int i=0;i<8-len;i++){
13                     res="0"+res;
14                 }
15                 res+="  ";
16                 String str=s.substring(start, start+16);
17                 char[] array=str.toCharArray();
18                 for(int i=0;i<array.length;i++){
19 //                    res+=Integer.toHexString((int)array[i])+"  ";
20                     
21                     if((i+1)%8==0){
22                         res+=Integer.toHexString((int)array[i])+"  ";
23                     }else{
24                         res+=Integer.toHexString((int)array[i])+" ";
25                     }
26                     
27 //                    if(i%8==0){
28 //                        res+=Integer.toHexString((int)array[i])+"  ";
29 //                    }else{
30 //                        res+=Integer.toHexString((int)array[i])+" ";
31 //                    }
32                 }
33                 res+=str;
34                 System.out.println(res);
35                 
36                 start+=16;
37             }
38         }
39         sc.close();
40     }
41 }

 

我自己想的代码,没有想清楚,效果比较差(没用)

 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     public static void main(String[] args){
 5         Scanner sc=new Scanner(System.in);
 6         String str=sc.nextLine();
 7         String[] array={"61","62","63","64","65","66","67","68","69","6a"
 8                 ,"6b","6c","6d","6e","6f","70","71","72","73","74","75",
 9                 "76","77","78","79","7a"};
10         
11         for(int i=0;i<str.length()/16;i++){
12             //System.out.printf("%010x\n",i);
13             System.out.print("000000"+i+"0");
14             System.out.print("  ");
15             
16             for(int j=i*16;j<(i+1)*16;j++){
17                                 
18                 if(j<26){
19 //                    System.out.print(j+" ");
20                     System.out.print(array[j]+" ");
21                 }
22                 else {
23                     int k=j%26;
24 //                    System.out.print(k+" ");
25                     System.out.print(array[k]+" ");
26                 }
27                 
28             }
29             
30             System.out.print("  ");
31             String s=str.substring(i*16, (i+1)*16);
32             System.out.println(s);
33         }
34     }
35 }

 

posted @ 2017-04-05 08:39  临江仙zhe  阅读(83)  评论(0)    收藏  举报