十进制到二进制转换器循环

时间:2014-10-14 19:08:30

标签: java for-loop

我应该创建一个程序,该程序使用用户输入的十进制数,并使用内部和外部for或while循环将其转换为二进制数。

出于某种原因,每当我输入正整数时,程序就什么都不做。

我的代码:

public class Dec2Bin
{
    public static void main (String[] args)
    {
        System.out.println("Welcome to Decimal Number to Binary Converter");
        System.out.println("");

        Scanner s = new Scanner(System.in);
        while (true)
        {
            String binary = "";
            System.out.print("Enter a decimal number (-1 to end)");
            int input = s.nextInt();

            if (input <= 0)
            {
                System.out.println("Goodbye!");
                break;
            }

            int result = input;

            while (result >= 0)
            {
                result = result / 2;
                binary = (result % 2) + binary;
            }
            System.out.println("Binary number is " + binary);
        }
    }
}

每次应该将结果%2中的余数添加到二进制字符串中,该字符串应显示转换后的二进制数。它确实有效,如果我输入一个负数,显示&#34;再见!&#34;信息。不知道我哪里出错了。

3 个答案:

答案 0 :(得分:2)

你的问题是由

引起的无限循环
while (result >= 0)
{
    result = result / 2;
    binary = (result % 2) + binary;
}

更改您的

while (result >= 0)

while (result > 0)

并测试当前代码不起作用(例如小数1应该返回1而不是0

答案 1 :(得分:2)

将您的代码更改为:

        Scanner s = new Scanner(System.in);
        while (true)
        {
            // String binary = ""; --> do not need this
            System.out.print("Enter a decimal number (-1 to end)");
            int input = s.nextInt();

            if (input < 0) {
                System.out.println("Goodbye!");
                break;
            } else {
            System.out.print("Binary number is ");

            int binary[] = new int[8];  // ------> assume 8-bit, you can make it 16, 32, or 64
            int count = 0;
            while( input != 0 ) {
                binary[count++] = input % 2;//--> this code is equivalent to: 
                input = input / 2;          //    binary[count] = input % 2;
            }                               //    count++
            for (int i = 7; i >= 0; i--) {  // printing the binary array backwards
                System.out.print(binary[i]);
            }
        }
            System.out.print("\n\n");
        }

答案 2 :(得分:0)

更改此2个命令的顺序。

按此顺序保留

binary =(结果%2)+二进制; result = result / 2;

完整代码

import java.util.Scanner;

public class DecimalToBinary {

    public static void main (String[] args)
    {
        System.out.println("Welcome to Decimal Number to Binary Converter");
        System.out.println("");

        Scanner s = new Scanner(System.in);
        while (true)
        {
            String binary = "";
            System.out.print("Enter a decimal number (-1 to end)");
            int input = s.nextInt();

            if (input <= 0)
            {
                System.out.println("Goodbye!");
                break;
            }

            int result = input;

            while (result > 0)
            {
                binary = (result % 2) + binary;
                result = result / 2;
            }
            System.out.println("Binary number is " + binary);
        }
    }
}