八進(jìn)制轉(zhuǎn)十進(jìn)制【限制int范圍內(nèi)】
Posted on 2006-12-05 11:34 Yemoo'S Java Blog 閱讀(2036) 評(píng)論(0) 編輯 收藏 所屬分類(lèi): JAVA個(gè)人敝作
/**
?*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);
?????}
?}
?*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);
?????}
?}