本文介绍了在 Java 中反转数字的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试做一个数字系统计算器,但只使用控制或重复结构.这是我的示例:
I'm trying to do a number system calculator but only using the control or repetitive structures. Here's my sample:
int base = 0, given = 0, remainder = 0;
// input given here
System.out.print("The answer is: ");
if (base == 2){
while(given != 0){
remainder = given % base;
given /= base;
System.out.print("" + remainder);
}
}
输出如下:
Input: 32
The answer is: 000001
问题是,由于 32 的二进制是 100000 而不是 000001,我如何将输出反转为 100000?条件是除了三个重复结构:for、while 和 do-while 和使用决定性结构:if、else-if 和 for 外,不要使用任何东西.
The question is, how would I reverse the output to 100000 since the binary of 32 is 100000 and not 000001? The condition is not to use anything except the three repetitive structures: for, while and do-while and using decisive structures: if, else-if and for.
推荐答案
您可以将数字连接成一个 String
并在循环后打印:
You can concatenate the digits into a String
and print it after the loop:
String output = "";
if (base == 2){
while(given != 0){
remainder = given % base;
given /= base;
output = remainder + output;
}
System.out.println(output);
}
这篇关于在 Java 中反转数字的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!