Java 无法获取二进制文件以水平打印并停止执行

Java 无法获取二进制文件以水平打印并停止执行,java,printing,binary,while-loop,Java,Printing,Binary,While Loop,我正在制作一个从十进制转换为二进制、八进制和十六进制的程序。到目前为止,我只关注十进制到二进制的部分。我的问题是,当我要求二进制文件转换为上述数字时,它会垂直打印,而不是像010那样水平打印。此外,如果y输入大于1024,则我的while语句不会停止执行,这是我希望能够接受的最高值 import java.util.Scanner; public class DNS { public static void main(String[] args) { int y

我正在制作一个从十进制转换为二进制、八进制和十六进制的程序。到目前为止,我只关注十进制到二进制的部分。我的问题是,当我要求二进制文件转换为上述数字时,它会垂直打印,而不是像010那样水平打印。此外,如果y输入大于1024,则我的while语句不会停止执行,这是我希望能够接受的最高值

import java.util.Scanner;

public class DNS
{
    public static void main(String[] args)
    {
        int y;
        Scanner input = new Scanner( System.in);
    do
    {   
        System.out.println("java DisplayNumberSystems");
        System.out.println("Enter a decimal value to display to: ");
        y = input.nextInt();

        for(int x=0; x <=y; x++)
        {
            convertToBinary(x);
        }
    }
    while(y <=1024);    

    }

    public static void convertToBinary(int x)
    {
        if(x >0)
        {
            convertToBinary(x/2);
            System.out.print(x%2 + " ");

        }
        System.out.println("");
    }

}
import java.util.Scanner;
公共类DNS
{
公共静态void main(字符串[]args)
{
int-y;
扫描仪输入=新扫描仪(System.in);
做
{   
System.out.println(“java DisplayNumberSystems”);
System.out.println(“输入要显示的十进制值:”);
y=输入。nextInt();

对于(int x=0;x从
public static void convertToBinary(int x)
您将能够水平打印

并将您的do while更改为这样的简单while

    int y;
    Scanner input = new Scanner( System.in);
    System.out.println("java DisplayNumberSystems");
    System.out.println("Enter a decimal value to display to: ");
    y = input.nextInt();

    while(y <=1024)
{   

    for(int x=0; x <=y; x++)
    {
        convertToBinary(x);
    }
}
inty;
扫描仪输入=新扫描仪(System.in);
System.out.println(“java DisplayNumberSystems”);
System.out.println(“输入要显示的十进制值:”);
y=输入。nextInt();

虽然(y您可能希望在主
for
循环中调用
convertToBinary(x)
之后调用空的
System.out.println()
调用(顺便说一句,您可以不带参数地调用它),否则在每个被计算数字的每个递归步骤中都会打印空行

for (int x = 0; x <= y; x++) {
    convertToBinary(x);
    System.out.println();
}

该方法递归地调用自身,您在每个步骤中调用
println()
println()
打印新行。
while (true) {
    System.out.println("java DisplayNumberSystems");
    System.out.println("Enter a decimal value to display to: ");
    y = input.nextInt();

    if (y < 0) {
        System.out.println("That number is not positive!");
        break;
    }

    if (y > 1024) {
        System.out.println("That number is too big!");
        break;
    }

    for (int x = 0; x <= y; x++) {
        convertToBinary(x);
        System.out.println();
    }
}