英文:
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 = "";
int saveInt, intValue, quotient, remainder;
int cnt = 0;
System.out.println ("\n(7) Converting an unsigned Integer to binary.");
System.out.print ("Enter an integer value: ");
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) //I tried this to split btwn every 8 spaces.
//Not sure how to space after first digit.
System.out.print(" ");
}
System.out.printf ("After Conversion: %d(10) = %s(2).\n", 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("(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();
}
Output:
(7) Converting an unsigned Integer to binary.
Enter an integer value: 65536
After Conversion: 65536(10) = 1 00000000 00000000(2).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论