字符和数字之间的转换

自己用JAVA写的,第一个正式的“作品”。

 1 // 字符和数字之间的转换
 2 import  java.io. * ;
 3
 4 class  JavaBase
 5 {
 6        public static void main( String [] args ) throws IOException
 7        {    
 8                showMenu();
 9                String str = null;
10                do
11                {
12                        byte [] bytes = new byte[10];
13                        int ch = System.in.read( bytes );
14                        str = new String( bytes, 0, ch-2 );
15                        int choices = Integer.parseInt( str );
16                        switch( choices )
17                        {
18                                case 1:
19                                System.out.println( "The Char-Word Unicode:" );
20                                System.out.println( "Please input the char you want to analyse:" );
21                                System.out.println( "Ends With the Char 'q' " );
22                                toNumer( System.in );
23                                System.out.println( "Now Back the menu" );
24                                showMenu();
25                                break;
26                                case 2:
27                                System.out.println( "The Num To The Char Unicode:" );
28                                System.out.println( "Please input the num you want to analyse:" );
29                                System.out.println( "Ends With the Char 'q' " );
30                                toChar( System.in );
31                                showMenu();
32                                break;
33                                case 3:
34                                System.out.println( "You choice to quit " );;                                
35                                return;
36                                default:
37                                System.out.println( "Please choise again :" );
38                                showMenu();                                
39                        }

40                }

41                while( str.compareTo( "quit" ) != 0   )    ;            
42        }

43        
44        static void showMenu()
45        {
46                System.out.println( "Please Input your Choice:" );
47                System.out.println( "1.Char To Num;2.Num To Char;3.Quit." );
48        }

49        
50//        //字母转换数字
51        static void toNumer( InputStream is )
52        {
53                int x = 0;
54                do
55                {
56                        try
57                        {
58                                x = is.read();
59                                if( x !='\r' && x != '\n'  )
60                                        System.out.println( x );
61//                                if( x =='\r' )
62//                                        System.out.println( 13 );
63//                                if( x == '\n' )
64//                                        System.out.println( 10 );            
65                        }

66                        catch( Exception e )
67                        {
68                        //System.out.println( e.toString() );
69                        }
                        
70                }

71                while( x != 'q' );
72        }
    
73        
74            //convert the num to char
75        static void toChar( InputStream is )
76        {    
77                String str = null;
78                    do
79                    {
80                            try
81                            {
82                                byte [] bytes = new byte[20];
83                                int length = is.read( bytes );
84                    //            System.out.println( "The leagth : " + length );
85                                str = new String( bytes,0,length-2 );
86                    //            System.out.println( "The String is : " + str );
87                    //            System.out.println( "The String length : " + str.length() );                            
88                              int num = Integer.parseInt( str, 10 );                            
89                              System.out.println( (char)num );
90                          }

91                         catch( Exception e )
92                         {
93                         //     System.out.println( e.toString() );
94                         }
                        
95            }

96        while( str.compareTo( "q" )!= 0 );
97        }

98}

你可能感兴趣的:(转换)