提问人:Limbo 提问时间:2/4/2022 更新时间:2/4/2022 访问量:141
Java 程序不会显示整个输出 - 输入整数的反向
Java program won't show the entire output - the reverse of an inputted integer
问:
我有一个程序,旨在获取用户的输入(两位数整数)并输出它的反面。虽然它编译时没有错误,但它只输出第一个整数,而不输出第二个整数。
程序代码:
//Set variable
int reversed = 0;
int requiredLength = 2;
//Get User Input
Scanner sc = new Scanner(System.in);
System.out.println("Enter a two-digit number:");
while (sc.hasNext()) {
//Conditional for User Input
if (sc.hasNextInt()) {
//Set scanner variables
int num = sc.nextInt();
int numLength = String.valueOf(num).length();
if (numLength == requiredLength) {
//Reverse integer
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
System.out.println("Reversed Number: " + reversed);
break;
} else {
//Error message
System.out.println("Error: Input integer with two-digits");
System.out.print("Please, enter a two-digit number:\n");
}//End of conditional
} else {
//Error message
System.out.println("Error: Input not an integer");
System.out.print("Please, enter a two-digit number:\n");
sc.next();
}//End of conditional
}//End of while loop
首选输出:
Please enter a two-digit number:
45
Reversed: 54
实际输出
Enter a two-digit number:
45
Reversed: 5
怎么了?
答:
1赞
Zag
2/4/2022
#1
你的问题就在这里
if (numLength == requiredLength) {
//Reverse integer
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
System.out.println("Reversed Number: " + reversed);
break;
}
我怀疑你的意思是把它包含在数字的循环中,然后与输入的循环混淆了。既然你知道它只是两位数,你就可以这样做。
if (numLength == requiredLength) {
//Reverse integer
int digit = num % 10;
int reversed = 10 * digit + num / 10;
System.out.println("Reversed Number: " + reversed);
break;
}
0赞
VAIBHAV NIRMAL
2/4/2022
#2
你的问题解决了!!你忘了在那里写 while 循环:
while (num!=0){
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
这个 while 循环每次将数字除以 10,并在每个循环中将数字添加到反向数字中,而数字将等于 0,则循环将中断
现在这是您的整个工作代码:
int reversed = 0;
int requiredLength = 2;
//Get User Input
Scanner sc = new Scanner(System.in);
System.out.println("Enter a two-digit number:");
while (sc.hasNext()) {
//Conditional for User Input
if (sc.hasNextInt()) {
//Set scanner variables
int num = sc.nextInt();
int numLength = String.valueOf(num).length();
if (numLength == requiredLength) {
//Reverse integer
while (num!=0){
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
System.out.println("Reversed Number: " + reversed);
break;
} else {
//Error message
System.out.println("Error: Input integer with two-digits");
System.out.print("Please, enter a two-digit number:\n");
}//End of conditional
} else {
//Error message
System.out.println("Error: Input not an integer");
System.out.print("Please, enter a two-digit number:\n");
sc.next();
}//End of conditional
}//End of while loop
0赞
Jagadeesh mano
2/4/2022
#3
这个逻辑必须遍历输入数字的每个数字,所以试着把它加到循环中
while(number != 0){
mod = number % 10;
number = number / 10;
result = result * 10 + mod;
}
0赞
Cheng Thao
2/4/2022
#4
如果是数字,则 n%10 为您提供正确的数字,并为您提供左数字。n
n/10
要将它们组合成一个数字,您需要将右边的数字向左移动,方法是将其乘以 。然后从左边添加数字。10
int n = 31;
if (n/10 < 10){
System.out.println(n%10*10 + n/10);
}
输出:
31
13
评论
0
String converted = num + ""; System.out.println("Reversed Number: " + converted.charAt(1) + converted.charAt(0));