十进制到二进制,打印错误的答案

时间:2016-02-10 07:13:44

标签: java binary stack decimal

嘿,我想知道是否有人可以发现我的代码有问题?如果你能帮我解释一下吧!当我输入99时我得到1100 011,它应该是0110 0011.

import java.util.*;
public class SchoolHomework {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    System.out.println("Program that converts decimal to binary!");
    int dec;
    System.out.println("Please type in a decimal number:");
    Scanner input = new Scanner(System.in);
    Stack<Integer> todec = new Stack<Integer>();
    dec = input.nextInt();
    if (dec < 0){
        System.out.println("Error: Please enter a positive number!");
        System.exit(0);
    }
    while (dec != 0){
        int stackv = dec % 2;
        todec.push(stackv);
        dec /= 2;

    }
    System.out.println(dec + " To binary is: ");
    int counter = 0;
    while (!(todec.isEmpty() )) {
        String val = todec.pop().toString();
        System.out.print(val);
        counter = counter + 1;
        if (counter >= 4){
            counter = 0;
            System.out.print(" ");
        }

    }
}
}

3 个答案:

答案 0 :(得分:1)

你写的算法看起来非常好。你接近解决方案。最简单的方法是继续将零推到堆栈,直到达到4的长度倍数。如果你得到了很好的说明,请告诉我。)

import java.util.*;
public class SchoolHomework {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Program that converts decimal to binary!");
        int dec;
        System.out.println("Please type in a decimal number:");
        Scanner input = new Scanner(System.in);
        Stack<Integer> todec = new Stack<Integer>();
        dec = input.nextInt();
        if (dec < 0){
            System.out.println("Error: Please enter a positive number!");
            System.exit(0);
        }
        int size = 0;

        while (dec != 0){
            int stackv = dec % 2;
            todec.push(stackv);
            dec /= 2;
            size++;
        }
        if (size % 4 > 0) {
            for(int i = 0; i < 4 - (size % 4); i++) {
               todec.push(0);
            }
        }
        System.out.println(dec + " To binary is: ");
        int counter = 0;
        while (!(todec.isEmpty() )) {
            String val = todec.pop().toString();
            System.out.print(val);
            counter = counter + 1;
            if (counter >= 4){
                counter = 0;
                System.out.print(" ");
            }

        }
    }
}

答案 1 :(得分:0)

这是因为您在打印时从左到右插入空格。如果你想获得所需的输出,那么我建议你开始在Stack中插入空格。

以下是快速代码段:

public void onTextChanged(CharSequence s, int start, int before,
    int count) {
        //YOUR CODE
        if (atv_search.enoughToFilter()) {
            atv_search.showDropDown();
        }
    }

输出:

public static void main(String[] args) {
    System.out.println("Program that converts decimal to binary!");
    System.out.println("Please type in a decimal number:");

    Scanner input = new Scanner(System.in);
    Stack<Character> todec = new Stack<Character>();
    int dec = input.nextInt();
    if (dec < 0){
        System.out.println("Error: Please enter a positive number!");
        System.exit(0);
    }

    int counter = 0;
    System.out.println(dec + " To binary is: ");
    while (dec > 0){
        int stackv = dec % 2;
        dec /= 2;
        todec.push((char)(stackv + '0'));

        counter++;
        if (counter%4 == 0){
            todec.push(' ');
        }
    }

    /* Pad Extra Zeors */
    while (counter%4 != 0){
        counter++;
        todec.push('0');
    }

    /* Print Stack Values */
    while (!(todec.isEmpty() )) {
        String val = todec.pop().toString();
        System.out.print(val);
    }
}

答案 2 :(得分:0)

当推送到Stack的最后一个值为0(可能为1或更多)且Stack以少于8个条目结束时,会出现问题。满足条件dec != 0并且循环结束。您可以使用for循环来确保堆栈有8个条目

for (int i = 0 ; i < 8 ; ++i)
{
    int stackv = dec % 2;
    todec.Push(stackv);
    dec /= 2;
}