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

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

How To Print Spaces Between Text Output

问题

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

  1. String binaryStr = "";
  2. int saveInt, intValue, quotient, remainder;
  3. int cnt = 0;
  4. System.out.println ("\n(7) 将无符号整数转换为二进制。");
  5. System.out.print ("输入一个整数值:");
  6. intValue = kb.nextInt();
  7. if (intValue >= 0)
  8. {
  9. saveInt = intValue;
  10. while (intValue > 0)
  11. {
  12. remainder = intValue % 2;
  13. intValue = intValue / 2;
  14. binaryStr = (char)(remainder+48) + binaryStr;
  15. for (cnt = binaryStr.length(); cnt < 9; ++cnt) //我尝试这样在每8个空格之间分隔。
  16. //不确定如何在第一个数字后面添加空格。
  17. System.out.print(" ");
  18. }
  19. System.out.printf ("转换后:%d(10) = %s(2)。\n", saveInt, binaryStr);
  20. }

我感激任何帮助。谢谢!

英文:

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:

  1. String binaryStr = &quot;&quot;;
  2. int saveInt, intValue, quotient, remainder;
  3. int cnt = 0;
  4. System.out.println (&quot;\n(7) Converting an unsigned Integer to binary.&quot;);
  5. System.out.print (&quot;Enter an integer value: &quot;);
  6. intValue = kb.nextInt();
  7. if (intValue &gt;= 0)
  8. {
  9. saveInt = intValue;
  10. while (intValue &gt; 0)
  11. {
  12. remainder = intValue % 2;
  13. intValue = intValue / 2;
  14. binaryStr = (char)(remainder+48) + binaryStr;
  15. for (cnt = binaryStr.length(); cnt &lt; 9; ++cnt) //I tried this to split btwn every 8 spaces.
  16. //Not sure how to space after first digit.
  17. System.out.print(&quot; &quot;);
  18. }
  19. System.out.printf (&quot;After Conversion: %d(10) = %s(2).\n&quot;, saveInt, binaryStr);
  20. }

I appreciate any help. Thanks!

答案1

得分: 1

  1. public static void main(String... args) {
  2. Scanner scan = new Scanner(System.in);
  3. System.out.println("(7) Converting an unsigned Integer to binary.");
  4. System.out.print("Enter an integer value: ");
  5. int num = scan.nextInt();
  6. String binaryStr = convertToBinary(num);
  7. System.out.printf("After Conversion: %d(10) = %s(2).\n", num, binaryStr);
  8. }
  9. public static String convertToBinary(int num) {
  10. String str = Integer.toBinaryString(num);
  11. StringBuilder buf = new StringBuilder(str.length() + 5);
  12. for (int i = str.length() - 1, j = 0; i >= 0; i--, j++) {
  13. if (j > 0 && j % 8 == 0)
  14. buf.append(' ');
  15. buf.append(str.charAt(i));
  16. }
  17. return buf.reverse().toString();
  18. }
英文:
  1. public static void main(String... args) {
  2. Scanner scan = new Scanner(System.in);
  3. System.out.println(&quot;(7) Converting an unsigned Integer to binary.&quot;);
  4. System.out.print(&quot;Enter an integer value: &quot;);
  5. int num = scan.nextInt();
  6. String binaryStr = convertToBinary(num);
  7. System.out.printf(&quot;After Conversion: %d(10) = %s(2).\n&quot;, num, binaryStr);
  8. }
  9. public static String convertToBinary(int num) {
  10. String str = Integer.toBinaryString(num);
  11. StringBuilder buf = new StringBuilder(str.length() + 5);
  12. for (int i = str.length() - 1, j = 0; i &gt;= 0; i--, j++) {
  13. if (j &gt; 0 &amp;&amp; j % 8 == 0)
  14. buf.append(&#39; &#39;);
  15. buf.append(str.charAt(i));
  16. }
  17. return buf.reverse().toString();
  18. }

Output:

  1. (7) Converting an unsigned Integer to binary.
  2. Enter an integer value: 65536
  3. 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:

确定