英文:
why does my code not giving me reverse number accurately
问题
Java代码示例:在插入010时,显示为1而不是整数;在插入200时,只显示反向的2,而不是应该显示精确的002。如何获得精确的反向数字:
class Reverse {
public static void main(String args[]) {
int remainder, sum = 0;
int number = 454;
while (number > 0) // 循环在这里
{
remainder = number % 10; // 获取余数
sum = (sum * 10) + remainder;
number = number / 10;
}
System.out.println("Reverse" + sum);
}
}
英文:
Java Code Example: i am facing issue when i insert 010 it shows only 1 instead of whole number and when i insert 200 it gave me reverse number only 2 while it should show me exact 002 how can i get exact number in reverse:
class Reverse{
public static void main(String args[]){
int remander,sum=0;
int number=454;
while(n>0) // loop here
{
remander=n%10; // Getting remainder
sum=(sum*10)+remander;
number=number/10;
}
System.out.println("Reverse"+sum);
}
}
答案1
得分: 2
很明显,你对Java和编程都是新手。欢迎来到StackOverflow。我会给你一些建议,这些通常不是我提供的常规建议,但可以帮助你更快地提升。
-
不要将一个变量用于多个不同的目的。一个变量应该只用于一个目的。这意味着如果一个变量保存了你的输入,不要用中间值覆盖它。变量(相对而言)是廉价的,创建另一个变量。你可以通过将变量标记为
final
来练习这一点,这会阻止你在分配后更改它们的值。 -
使用有意义的变量名。"r"不是有意义的名字,"remainder"是有意义的名字。这不仅能够立即帮助其他人,而且在以后当你的程序不再新鲜在脑海中,并且你不记得"r"代表什么时,也会帮助你自己。
-
一旦你掌握了不重用变量的技巧,你会有很多变量。寻找那些只被设置一次并且只被读取一次的变量,尝试重新排列你的代码以完全移除它们。理解我所说的可能需要一些时间,但是如果你听说过"重构",这个想法就是去掉变量名,因为它没有被使用两次(所以它很可能不是你问题的关键部分)。
现在,应用这些规则:
final int number = 454;
// 获取数字的各个位,从最小到最大
int remainder = number;
int accumulator = 0;
while (remainder > 0) {
final int digit = remainder % 10;
accumulator = accumulator * 10 + digit;
remainder = remainder / 10;
}
final int reversed = accumulator;
注意digit
从未被使用两次:
final int number = 454;
// 获取数字的各个位,从最小到最大
int remainder = number;
int accumulator = 0;
while (remainder > 0) {
accumulator = accumulator * 10 + remainder % 10;
remainder = remainder / 10;
}
final int reversed = accumulator;
注意所有这些"逻辑"可能有一个非常好的名称,因此我们将创建一个"方法":
public int reverse(int value) {
int remainder = value;
int accumulator = 0;
while (remainder > 0) {
accumulator = accumulator * 10 + remainder % 10;
remainder = remainder / 10;
}
return accumulator;
}
这可以清理其他代码:
final int number = 454;
final int reversed = reverse(number);
甚至可能变成:
final int reversed = reverse(454);
英文:
It is clear that you are new to Java, and new to programming. Welcome to StackOverflow. I'm going to add some extra advice that I don't normally offer, to help you get better faster.
-
Don't reuse a variable for more than one thing. Use it for only one thing. That means that if a variable holds you input, don't overwrite it with intermediate values. Variables are (relatively) cheap, create another. You can practice this by marking your variables
final
which will prevent you from changing their values after assigned. -
Use meaningful variable names. "r" is not a meaningful name. "remainder" is a meaningful name. This helps others immediately, and will help you later, when the program isn't fresh on your mind and you don't remember what "r" means.
-
Once you master not reusing variables, you will have a lot of variables. Look for variables that are "set once and read once" and try to rearrange your code to remove them completely. It will take some time to understand what I mean, but if you have heard of "refactoring" the idea is to remove the variable name because it isn't used twice (so it probably isn't a key part of your problem.
Now, applying these rules
final int number = 454;
// get the number digits, smallest to largest
int remainder = number;
int accumulator = 0;
while (remainder > 0) {
final int digit = remainder % 10;
accumulator = accumulator * 10 + digit;
remainder = remainder / 10;
}
final int reversed = accumulator;
note that digit
is never used twice
final int number = 454;
// get the number digits, smallest to largest
int remainder = number;
int accumulator = 0;
while (remainder > 0) {
accumulator = accumulator * 10 + remainder % 10;
remainder = remainder / 10;
}
final int reversed = accumulator;
Note that all that "logic" could have a really good name put to it, so we'll create a "method"
public int reverse(int value) {
int remainder = value;
int accumulator = 0;
while (remainder > 0) {
accumulator = accumulator * 10 + remainder % 10;
remainder = remainder / 10;
}
return accumulator;
}
which cleans up the other code to
final int number = 454;
final int reversed = reverse(number);
or maybe even
final int reversed = reverse(454);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论