十进制转二进制,打印错误答案

Decimal to binary, prints wrong answers

嘿,我想知道是否有人能发现我的代码有问题吗?如果可以的话,请给我解释一下!当我输入 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(" ");
        }

    }
}
}

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

这是快速代码片段:

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

输出:

0110 0011

你写的算法看起来很不错。你离解决方案很近了。最简单的方法是继续将零压入堆栈,直到长度达到 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(" ");
            }

        }
    }
}

当推送到 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;
}