Java Home

Java技术修炼中...
posts - 20, comments - 22, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理
/**
 *Description:convert Oct to Dec
 *Author:yemoo 2006.12.05
 
*/

 
public   class  P38{
     
int  convertOct2Dec(String Oct){
         
int  result = 0 ;
         
int  power = 1 ;
         
for ( int  i = Oct.length() - 1 ;i >= 0 ;i -- ,power *= 8 ){
             
char  temp = Oct.charAt(i);
             
int  intTemp = Character.getNumericValue(temp);
             
if (intTemp >= 0 && intTemp < 8 ){
                 result
+= intTemp * power;
             }
else {
                 
if (temp == ' - ' && i == 0 ){
                     result
=- result;
                 }
else {
                     
return  result;
                 }
             }
         }
         
return  result;
     }

     String readInput(){
         KeyboardInput in
= new  KeyboardInput();
         System.out.print(
" Please input a october number: " );
         
return  in.readString();
     }

     
public   static   void  main(String args[]){
         P38 obj
= new  P38();
         String oct
= obj.readInput();
         
int  value = obj.convertOct2Dec(oct);
         System.out.println(
" Oct: " + oct + " ==Dec: " + value);
     }
 }

只有注册用户登录后才能发表评论。


网站导航: