字符和数字之间的转换

博主分享了自己用JAVA完成的第一个正式“作品”,并给出了转载链接https://www.cnblogs.com/yongboy/archive/2005/11/06/270240.html 。

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

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

40ExpandedSubBlockEnd.gif                }

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

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

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

66InBlock.gif                        catch( Exception e )
67ExpandedSubBlockStart.gifContractedSubBlock.gif                        dot.gif{
68InBlock.gif                        //System.out.println( e.toString() );
69ExpandedSubBlockEnd.gif                        }
                        
70ExpandedSubBlockEnd.gif                }

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

91InBlock.gif                         catch( Exception e )
92ExpandedSubBlockStart.gifContractedSubBlock.gif                         dot.gif{
93InBlock.gif                         //     System.out.println( e.toString() );
94ExpandedSubBlockEnd.gif                         }
                        
95ExpandedSubBlockEnd.gif            }

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

98ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/yongboy/archive/2005/11/06/270240.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值