[英]Reversing the output of numbers in 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);
}
}
And the output goes like this: 输出如下:
Input: 32
The answer is: 000001
The question is, how would I reverse the output to 100000 since the binary of 32 is 100000 and not 000001? 问题是,由于二进制数32是100000而不是000001,我将如何将输出反转为100000? 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. 条件是除了三个重复结构之外,不要使用其他任何东西:for,while和do-while,并使用决定性结构:if,else-if和for。
You can concatenate the digits into a String
and print it after the loop: 您可以将数字连接成一个String
并在循环后打印它:
String output = "";
if (base == 2){
while(given != 0){
remainder = given % base;
given /= base;
output = remainder + output;
}
System.out.println(output);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.