[英]Reversing digits in Java. Leading and trailing zeros won't print
The problem was to reverse user entered digits. 问题是要反转用户输入的数字。 I have it working but while testing it I realized that it won't print either leading or trailing zeros. 我可以使用它,但是在测试时,我意识到它不会打印前导零或尾随零。
For example if I enter 10 it only displays 1 in the result. 例如,如果我输入10,则结果中仅显示1。
If I enter 0110 I get a result of 11. 如果输入0110,则结果为11。
Here is my code: 这是我的代码:
public class ReversingDigits {
int value;
int reverse;
public ReversingDigits() {
value = 10;
reverse = 0;
}// end constructor
public void reverse() {
System.out.println("Enter a valid 2-4 digit number: ");
Scanner input = new Scanner(System.in);
value = input.nextInt();
if (value < 10 || value > 9999){
System.out.print("Please enter a valid 2-4 digit number: ");
value = input.nextInt();
}
while (value > 0) {
reverse *= 10;
reverse += value % 10;
value /= 10;
}
System.out.println("Reversed numbers are: " + reverse);
}
}//end class
Any ideas on how to get the zeros to print? 关于如何打印零的任何想法?
Thanks 谢谢
Make sure you work with a String while reversing your number. 确保在反转数字时使用字符串。 It will preserve leading zeros. 它将保留前导零。 As you know 00001 is the same as 1 when in int
representation, and so converting that to a string will remove all leading zeros. 如您所知,在int
表示中,00001与1相同,因此将其转换为字符串将删除所有前导零。
Here's your code sample modified to read a string from the input, and only convert it to an int when you need to check the range. 这是修改后的代码示例,以从输入中读取字符串,并且仅在需要检查范围时才将其转换为int。
public void reverse() {
System.out.println("Enter a valid 2-4 digit number: ");
Scanner input = new Scanner(System.in);
String value = input.next();
int valueInt = Integer.parseInt(value);
if (valueInt < 10 || valueInt > 9999){
System.out.print("Please enter a valid 2-4 digit number: ")
value = input.next();
}
String valueReversed = new StringBuilder(value).reverse().toString();
System.out.println("Reversed numbers are: " + valueReversed);
}
Note that in your code, if a user enters the wrong range twice in a row, your program won't prompt him again. 请注意,在您的代码中,如果用户连续两次输入错误的范围,则程序将不会再次提示他。 You may want to put this part of the code into a do-while
loop which only exits when the input range is correct. 您可能希望将这部分代码放入do-while
循环中,该循环仅在输入范围正确时退出。 Example 例
do {
System.out.print("Please enter a valid 2-4 digit number: ")
value = input.next();
int valueInt = Integer.parseInt(value);
} while (valueInt < 10 || valueInt > 9999);
//only get here when inputted value finally within target range.
Edit: As mentioned by @Levenal, you may also want to wrap Integer.parseInt
in a try/catch
block for NumberFormatException
in the event the user passes in a non-numerical input. 编辑:如@Levenal所述,在用户传递非数字输入的情况下,您可能还希望将Integer.parseInt
包装在NumberFormatException
的try/catch
块中。
As has been pointed out, reversing numbers you are much better off reversing a string. 正如已经指出的,反转数字比反转字符串要好得多。 If you are allowed to stray away from console input, JOptionPane is quite good for simple String input, like so: 如果允许您远离控制台输入,那么JOptionPane对于简单的String输入非常有用,如下所示:
while(true){
String input = JOptionPane.showInputDialog("Please anter a number between 10 & 9999: ");
if(input == null){//If input cancelled
break; //Exit loop
} else if(input.matches("\\d{2,4}")){//Regex for at least 2 but no more than 4 numbers
System.out.println(new StringBuilder(input).reverse().toString());//Reverse
break;
}
}
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.