有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我应该创建一个程序,使用用户输入的十进制数,并使用内部和外部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的余数前置到二进制字符串,该字符串应显示转换后的二进制数。如果我输入一个负数,并显示“再见”,它就会正常工作消息不知道我哪里出错了


共 (3) 个答案

  1. # 1 楼答案

    更改这两个命令的顺序

    保持秩序

    二进制=(结果%2)+二进制; 结果=结果/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);
            }
        }
    }
    
  2. # 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");
            }
    
  3. # 3 楼答案

    你的问题是由

    while (result >= 0)
    {
        result = result / 2;
        binary = (result % 2) + binary;
    }
    

    换衣服

    while (result >= 0)
    

    while (result > 0)
    

    并在当前代码不工作时进行测试(例如,十进制1应该返回1,而不是0