[英]Convert Decimal to Binary using Recursion Java
我試圖通過遞歸簡單地轉換為二進制。 我在返回聲明時遇到問題。 這會編譯但在運行時會出現溢出錯誤。 我不知道要返回什么(或者如果我的陳述是錯誤的)來防止這個錯誤。
謝謝!
public static String convertToBinary(int number)
{
if(number > 0)
{
convertToBinary(number / 2);
convertToBinary((number % 2 ));
}
return convertToBinary((number));
}
我相信您的問題是在 number/2 和 number%2 上調用 convertToBinary。 這段代碼對我來說很好用,與你所擁有的沒有什么不同:
import java.util.Scanner;
public class DecToBin {
public static void main(String[] args) {
int input;
Scanner scan = new Scanner(System.in);
System.out.print("Enter number to convert to binary: ");
input = scan.nextInt();
convert(input);
}
public static void convert(int num) {
if (num>0) {
convert(num/2);
System.out.print(num%2 + " ");
}
}
}
嗯,問題似乎是你實際上並沒有在你的遞歸方法中做任何事情。
在最基本的形式中,你的遞歸方法應該包含:
(這是一個過於簡單化的觀點,但現在就可以了。)
問題是你缺少一些轉義條件來處理參數只有一點長的情況,也就是你不能再細分它的時候。
您的代碼的另一個問題是您沒有對遞歸調用的結果做任何事情。 您應該存儲並連接它們。
我建議你重新開始:寫一個先轉換單個位的方法(這將是非遞歸的),然后將遞歸添加到它。 (一般建議:不要害怕丟棄代碼並從頭開始。)
假設這是家庭作業,我會指出主要錯誤..
return convertToBinary((number));
返回應該返回一個值,而不是調用 function。這只會添加一個遞歸的 state 堆棧,這會導致您的溢出。 嘗試將之前調用的值保存到一個變量中並返回它。
一旦number
達到零,該方法將簡單地一遍又一遍地調用自己。 最后的return
需要返回其他東西——比如一個字符串。 話雖如此,我認為這種方法並不是非常理想。
嘗試以下 - :
public static String dec2Bin(int num) {
String result = ((num % 2 == 0) ? "0" : "1"); // expr
if (abs(num) > 1) {
result = dec2Bin(num / 2) + result;
}
return result;
}
這行得通,但你必須從頭開始打印
static void printBinary(int x){
if(x==0)System.out.printf("%3d", x);
else{
System.out.printf("%3d",x%2);
printBinary(x/2);
}
}
以下將遞歸工作。 如果數字是負數,它將添加“-”作為結果的前綴。
void printBinary (int n) {
if (n < 0) { //base case
System.out.print("-");
printBinary(-n);
} else if (n < 2) { //base case
System.out.print(n);
return;
} else {
printBinary(n/2); //recursive step
int answer = n%2;
System.out.print(answer);
}
}
class DecBin {
static int convert(int i) {
if (i > 0) {
convert (i/2);
System.out.println(i%2);
return 0;
} else {
return 0;
}
}
public static void main(String[] args) {
DecBin.convert(10);
}
}
class Example{
public static String binary(int num){
String b = " "; // create empty string to concat the binary values
if (num == 0) {
return "0"; // return 0 for decimel 0
} else if(num == 1) {
return "1"; // return 1 for decimel 1
} else {
return b + binary(num/2)+(num%2);
}
}
public static void main(String args[]){
System.out.println(binary(128)); //print 10000000
System.out.println(binary(64)); //print 1000000
System.out.println(binary(1)); //print 1
System.out.println(binary(0)); //print 0
}
}
我試圖創建一個通用子例程,它采用任何十進制 integer 並將其轉換為所需的 BASE。
private Integer convertToBaseN(int num,int n, int pow)
{
Integer r = num%n;
if(num < n)
return new Double((Math.pow(10, pow-1))*r.doubleValue()).intValue();
return convertToBaseN(num/n, n,pow+1)+
new Double(Math.pow(10, pow-1)*r.doubleValue()).intValue();
}
num :- Decimal No you want to convert.
n:- Base to which you want to convert ( n =2 in your case).
pow = 1 (fixed);
Input=> convertToBaseN(503,5, 1); Output=> 4003
Input=> convertToBaseN(7,2, 1); Output=> 111
注意:- 它不適用於負數。
只需將 (number/2*10) 的二進制轉換添加到數字的余數:
int binary(int dec) {
int remainder=dec%2;
if (dec==1 || dec==0)
return dec;
else
return remainder + (binary(dec/2)*10);
}
public class DecImalToBinary {
public static String decimalToBinary(int num){
StringBuilder sb= new StringBuilder();
if (num <2){
return ""+ num;
}
else{
return (sb.append(num%2)) + decimalToBinary((num/2));
}
}
public static void main(String[] args){
System.out.println(decimalToBinary(8));
}
}
public String convertirABinario(int n){
String b = "";
if (n == 0 || n == 1){
b = "" + n;
}
else{
b = b + convertirABinario(n/2) + n%2;
}
return b;
}
這是我的解決方案:
public static String binaerRec(int number)
{
if (number > 1)
{
return binaerRec(number / 2) + number % 2;
} else
{
return 1 + "";
}
}
玩得開心
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.