簡體   English   中英

使用堆棧將整數轉換為二進制

[英]Converting integer to binary using a Stack

我是編程新手,但仍然想盡一切辦法。

我正在嘗試將一些余數壓入堆棧,然后彈出堆棧以二進制形式輸出數字。

import java.util.Stack;

public class BinaryNumber {
    private static Stack<Integer> stack = new Stack<Integer>();
    private static int remainder;

public static String binaryNum(int number){
        while (number > 0){ //while number > 0
            if ((number % 2 != 1) && (number % 2 != 0)){ //if number is not equal to zero or 1
                number = (number-1); //update number to one less
                int remainder = 1;
            }
            else{
            int remainder = number % 2; //get the remainder
            }
            stack.push(new Integer(remainder));
            number /= 2;
        }
        stack.push(new Integer(1));
        return "hello".toString(); //just a test
    }
public static void printStack(){
    while (!stack.isEmpty()){
        stack.pop();
    }
}
    public static void main(String[]args){
        binaryNum(20);
        printStack();
    }
}

我似乎沒有任何輸出。 我已嘗試在紙上解決該問題,但無法弄清楚哪里出了問題。 我確實有一些println語句中出現較早,它似乎我原來如果聲明binaryNum總是越來越叫什么名字?

謝謝。

您需要修改代碼,如下所示:import java.util.Stack;

public class BinaryNumber {
    private static Stack<Integer> stack = new Stack<Integer>();
    private static int remainder;

public static String binaryNum(int number){
        while (number > 0){ //while number > 0
            if ((number % 2 != 1) && (number % 2 != 0)){ //if number is not equal to zero or 1
                number = (number-1); //update number to one less
                **remainder = 1;**
            }
            else{
                **remainder = number % 2; //get the remainder**
            }
            stack.push(new Integer(remainder));
            number /= 2;
        }
        stack.push(new Integer(1));
        return "hello".toString(); //just a test
    }
public static void printStack(){
    while (!stack.isEmpty()){
        **System.out.println(stack.pop());**
    }
}
    public static void main(String[]args){
        binaryNum(20);
        printStack();
    }
}

您正在做的是初始化兩個局部變量“ remainder”。 而添加到堆棧對象的是全局變量。

您不做任何打印-這就是為什么沒有輸出的原因!

試試這個(它確實有效):

import java.util.Stack;

public class BinaryNumber {
    private static Stack<Integer> stack = new Stack<Integer>();

    public static void binaryNum(int number) {
        // Important! In case you call binaryNum twice without calling printStack in between.
        stack.clear();
        while (number > 0) { // while number > 0
            int remainder = number % 2; // get the remainder
            stack.push(new Integer(remainder));
            number /= 2;
        }
    }

    public static void printStack() {
        while (!stack.isEmpty()) {
            System.out.print(stack.pop());
        }
    }

    public static void main(String[] args) {
        binaryNum(20);
        printStack();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM