简体   繁体   English

使用堆栈将整数转换为二进制

[英]Converting integer to binary using a Stack

I'm new to programming and still rying to get my head around this. 我是编程新手,但仍然想尽一切办法。

I'm trying to push some remainders onto the stack and then pop the stack to give the output of the number in binary. 我正在尝试将一些余数压入堆栈,然后弹出堆栈以二进制形式输出数字。

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();
    }
}

I seem to not be getting any output. 我似乎没有任何输出。 I've tried working through the problem on paper and can't figure out where it's failing. 我已尝试在纸上解决该问题,但无法弄清楚哪里出了问题。 I did have some println statements in there earlier and it seemed my original if statement in binaryNum was always getting called? 我确实有一些println语句中出现较早,它似乎我原来如果声明binaryNum总是越来越叫什么名字?

Thanks. 谢谢。

You need to modify code as follows: import java.util.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
                **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();
    }
}

What you are doing is initializing two local variables 'remainder'. 您正在做的是初始化两个局部变量“ remainder”。 And what gets added to the stack object is the global variable. 而添加到堆栈对象的是全局变量。

You don't do any printing - that's why there is no output! 您不做任何打印-这就是为什么没有输出的原因!

Try this (it actually works): 试试这个(它确实有效):

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