简体   繁体   中英

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?

Thanks.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM