Java實現(xiàn)任意進(jìn)制轉(zhuǎn)換
本文實例為大家分享了Java實現(xiàn)任意進(jìn)制轉(zhuǎn)換的具體代碼,供大家參考,具體內(nèi)容如下
問題描述
編寫程序?qū)崿F(xiàn)任意進(jìn)制間的相互轉(zhuǎn)換
(一)、進(jìn)制轉(zhuǎn)換思想
1、先把任意進(jìn)制轉(zhuǎn)化為十進(jìn)制
2、再把十進(jìn)制轉(zhuǎn)化為任意進(jìn)制
3、本算法結(jié)合了十以下進(jìn)制利用公式轉(zhuǎn)換以及十以上進(jìn)制調(diào)用函數(shù)轉(zhuǎn)換
(二)、問題分析
1.輸入當(dāng)前數(shù)進(jìn)制
輸入當(dāng)前進(jìn)制 m ,且保證 m>1
public static void main (String[] args){ int l,m,n; String l16; Scanner sc = new Scanner(System.in); System.out.println("請問當(dāng)前數(shù)為幾進(jìn)制(m>1)"); m = sc.nextInt();
2.輸入當(dāng)前數(shù)
(1)m進(jìn)制數(shù)要保證每個數(shù)字小于 m
System.out.println("請輸入當(dāng)前數(shù),每個數(shù)字均要小于"+m); int temp=0,i=0;
(2)對于十以下進(jìn)制,輸入的是數(shù)值型,采用 l=sc.nextInt();
(3)利用公式計算轉(zhuǎn)化為十進(jìn)制,可調(diào)用 Math 的 pow(m,i) 函數(shù),用以計算 m 的 i 次冪;數(shù)值的每個數(shù)字從右到左依次與 m^i (i=0,1,2……)相乘,可利用除10求余(%)運(yùn)算;每計算一次,原數(shù)據(jù)縮小十倍且只保留整數(shù)部分(除運(yùn)算 :/ ),直到數(shù)值為0結(jié)束
if(m<=10){ l=sc.nextInt(); while(l!=0){ temp+=(l%10)*Math.pow(m,i); i+=1; l=l/10; } }
(4)對于大于十的進(jìn)制,輸入的是字符串型,采用 l16=sc.next();
(5)可以直接調(diào)用 把任意進(jìn)制轉(zhuǎn)化為十進(jìn)制 的包Integer.valueOf()
else{ l16=sc.next(); temp=Integer.valueOf(l16,m); }
(6)所以實際上這里面其實只用 temp=Integer.valueOf(l16,m); 就可以解決整個問題,因為十以下進(jìn)制的數(shù)也能看成字符串型,這里為了介紹兩種方法
4.輸入待轉(zhuǎn)化進(jìn)制
這里仍然介紹兩種方法,十以下進(jìn)制、十以上進(jìn)制分開處理
System.out.println("請問要轉(zhuǎn)換為幾進(jìn)制(n>1)"); n = sc.nextInt();
(1)十以下進(jìn)制采用公式,并把該處理方法封裝在一個類(ConversionN)里
(2)temp 為上一段轉(zhuǎn)化后的十進(jìn)制數(shù),n 為待轉(zhuǎn)進(jìn)制
(3)十進(jìn)制 temp 轉(zhuǎn)化為 n 進(jìn)制的思想是:當(dāng)前數(shù)除 n 取余(% 運(yùn)算),得到的數(shù)字放在右邊第一位(相當(dāng)于該數(shù)字乘1);把該數(shù)縮小十倍保留整數(shù)部分,繼續(xù)除 n 取余,得到的數(shù)字放在右邊第二位(相當(dāng)于該數(shù)字乘10);依次循環(huán),把最終得到的數(shù)加在一起,就是轉(zhuǎn)化的 n 進(jìn)制數(shù)
public static int ConversionN(int temp,int n){ int l=0,j=1; while(n<=10 && temp!=0){ l+=(temp%n)*j; temp=temp/n; j=j*10; } return l; }
(4)十以上進(jìn)制直接調(diào)用 把十進(jìn)制轉(zhuǎn)換成任意進(jìn)制 的包 Integer.toString()
String L16=Integer.toString(temp,n); System.out.println("該數(shù)由"+m+"進(jìn)制轉(zhuǎn)化為"+n+"進(jìn)制的結(jié)果為:"+L16);
5.輸出轉(zhuǎn)化后數(shù)值
(1)由于選擇轉(zhuǎn)化的進(jìn)制不同(十以下,十以上),所以輸出的路徑也不同
(2)采用三目運(yùn)算符根據(jù)范圍選擇路徑:十進(jìn)制以下路徑1,十進(jìn)制以上路徑2,n <=1 路徑3(提示輸入有誤)
(3)結(jié)合 Switch 語句,完成選擇語句
int a = n<=10 & n>1 ? 1: n>10 ? 2:3; switch (a){ case 1 : BaseConversion base = new BaseConversion(); System.out.println("該數(shù)由"+m+"進(jìn)制轉(zhuǎn)化為"+n+"進(jìn)制的結(jié)果為:"+base.ConversionN(temp,n)); break; case 2 : String L16=Integer.toString(temp,n); System.out.println("該數(shù)由"+m+"進(jìn)制轉(zhuǎn)化為"+n+"進(jìn)制的結(jié)果為:"+L16); break; case 3: System.out.println("輸入有誤"); break; } }
(三)、完整代碼
把上述代碼整理
import java.util.*; public class BaseConversion { public static void main (String[] args){ int l,m,n; String l16; Scanner sc = new Scanner(System.in); System.out.println("請問當(dāng)前數(shù)為幾進(jìn)制(m>1)"); m = sc.nextInt(); System.out.println("請問要轉(zhuǎn)換為幾進(jìn)制(n>1)"); n = sc.nextInt(); System.out.println("請輸入當(dāng)前數(shù),每個數(shù)字均要小于"+m); int temp=0,i=0; if(m<=10){ l=sc.nextInt(); while(l!=0){ temp+=(l%10)*Math.pow(m,i); i+=1; l=l/10; } } else{ l16=sc.next(); temp=Integer.valueOf(l16,m); } int a = n<=10 & n>1 ? 1: n>10 ? 2:3; switch (a){ case 1 : BaseConversion base = new BaseConversion(); System.out.println("該數(shù)由"+m+"進(jìn)制轉(zhuǎn)化為"+n+"進(jìn)制的結(jié)果為:"+base.ConversionN(temp,n)); break; case 2 : String L16=Integer.toString(temp,n); System.out.println("該數(shù)由"+m+"進(jìn)制轉(zhuǎn)化為"+n+"進(jìn)制的結(jié)果為:"+L16); break; case 3: System.out.println("輸入有誤"); break; } } public static int ConversionN(int temp,int n){ int l=0,j=1; while(n<=10 && temp!=0){ l+=(temp%n)*j; temp=temp/n; j=j*10; } return l; } }
(四)運(yùn)行結(jié)果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java中初始化MediaRecorder的實現(xiàn)方法
這篇文章主要介紹了java中初始化MediaRecorder的實現(xiàn)方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-10-10Spring?boot?運(yùn)用策略模式實現(xiàn)避免多次使用if
這篇文章主要介紹了Spring?boot?運(yùn)用策略模式實現(xiàn)避免多次使用if,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09MyBatis中如何接收String類型的參數(shù)實現(xiàn)
這篇文章主要介紹了MyBatis中如何接收String類型的參數(shù)實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02詳解Java虛擬機(jī)管理的內(nèi)存運(yùn)行時數(shù)據(jù)區(qū)域
這篇文章主要介紹了詳解Java虛擬機(jī)管理的內(nèi)存運(yùn)行時數(shù)據(jù)區(qū)域的相關(guān)資料,需要的朋友可以參考下2017-03-03