如何在文本输出之间打印空格

huangapple go评论56阅读模式
英文:

How To Print Spaces Between Text Output

问题

程序的概念很简单:我只需要在每8个数字之间打印一个空格,而且第一个数字前面需要有一个空格。应该看起来像这样:65536(10) = 1 00000000 00000000。然而,我不确定如何编写这段代码。我尝试使用分割方法,但没有成功,现在我正在尝试这样做:

String binaryStr = "";
int saveInt, intValue, quotient, remainder;
int cnt = 0;
    
System.out.println ("\n(7) 将无符号整数转换为二进制。");
System.out.print   ("输入一个整数值:");
intValue = kb.nextInt();

if (intValue >=  0)
{
    saveInt = intValue;

    while (intValue > 0)
    {
        remainder = intValue % 2;
        intValue = intValue / 2;
        binaryStr = (char)(remainder+48) + binaryStr;
        for (cnt = binaryStr.length(); cnt < 9; ++cnt) //我尝试这样在每8个空格之间分隔。
        //不确定如何在第一个数字后面添加空格。
            System.out.print(" ");
    }
    System.out.printf ("转换后:%d(10) = %s(2)。\n", saveInt, binaryStr);
}

我感激任何帮助。谢谢!

英文:

The program is simple in concept: I simply need to print a space between every 8 numbers, and there needs to be a space in front of the first digit. It should look something like this: 65536(10) = 1 00000000 00000000. However, I am not sure how to write this code. I tried using the split method, but it didn't work, and now I'm trying this:

String binaryStr = &quot;&quot;;
int saveInt, intValue, quotient, remainder;
int cnt = 0;
    
System.out.println (&quot;\n(7) Converting an unsigned Integer to binary.&quot;);
System.out.print   (&quot;Enter an integer value: &quot;);
intValue = kb.nextInt();

if (intValue &gt;=  0)
	{
	saveInt = intValue;

	while (intValue &gt; 0)
		{
		remainder = intValue % 2;
		intValue = intValue / 2;
		binaryStr = (char)(remainder+48) + binaryStr;
     for (cnt = binaryStr.length(); cnt &lt; 9; ++cnt) //I tried this to split btwn every 8 spaces. 
     //Not sure how to space after first digit.
        System.out.print(&quot; &quot;);
		}
	System.out.printf (&quot;After Conversion: %d(10) = %s(2).\n&quot;, saveInt, binaryStr);
	}

I appreciate any help. Thanks!

答案1

得分: 1

public static void main(String... args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("(7) Converting an unsigned Integer to binary.");
    System.out.print("Enter an integer value: ");
    int num = scan.nextInt();
    String binaryStr = convertToBinary(num);
    System.out.printf("After Conversion: %d(10) = %s(2).\n", num, binaryStr);
}

public static String convertToBinary(int num) {
    String str = Integer.toBinaryString(num);
    StringBuilder buf = new StringBuilder(str.length() + 5);

    for (int i = str.length() - 1, j = 0; i >= 0; i--, j++) {
        if (j > 0 && j % 8 == 0)
            buf.append(' ');
        buf.append(str.charAt(i));
    }

    return buf.reverse().toString();
}
英文:
public static void main(String... args) {
    Scanner scan = new Scanner(System.in);
    System.out.println(&quot;(7) Converting an unsigned Integer to binary.&quot;);
    System.out.print(&quot;Enter an integer value: &quot;);
    int num = scan.nextInt();
    String binaryStr = convertToBinary(num);
    System.out.printf(&quot;After Conversion: %d(10) = %s(2).\n&quot;, num, binaryStr);
}

public static String convertToBinary(int num) {
    String str = Integer.toBinaryString(num);
    StringBuilder buf = new StringBuilder(str.length() + 5);

    for (int i = str.length() - 1, j = 0; i &gt;= 0; i--, j++) {
        if (j &gt; 0 &amp;&amp; j % 8 == 0)
            buf.append(&#39; &#39;);
        buf.append(str.charAt(i));
    }

    return buf.reverse().toString();
}

Output:

(7) Converting an unsigned Integer to binary.
Enter an integer value: 65536
After Conversion: 65536(10) = 1 00000000 00000000(2).

huangapple
  • 本文由 发表于 2020年10月10日 04:38:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64286996.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定