英文:
reversing a number using do-while
问题
import java.util.*;
public class Main {
static int replace(int number) {
if (number == 0)
return 0;
int digit = number % 10;
if (digit == 0)
digit = 1;
return (number / 10) * 10 + digit;
}
static int Convert(int number) {
if (number == 0)
return 0;
else
return replace(number);
}
public static void main(String[] args) {
int number;
Scanner kb = new Scanner(System.in);
System.out.print("Enter the number: ");
number = kb.nextInt();
System.out.println("replace:" + replace(number));
int a, m = 0, sum = 0;
do {
a = replace(number) % 10;
m = m * 10 + a;
sum = sum + a;
number = replace(number) / 10;
} while (replace(number) > 0);
System.out.println("Reverse:" + m);
System.out.println("Sum of digits:" + sum);
}
}
英文:
Create a java program that reads an integer number (NUM) and determine its reverse by using the division and remainder/modulo operators. If the last digit is zero, replace it with a one(1) before reversing the number. Output also the sum of all the digits.
import java.util.*;
public class Main {
static int replace(int number){
if (number == 0)
return 0;
int digit = number % 10;
if (digit == 0)
digit = 1;
return (number/10) * 10 + digit;
}
static int Convert(int number){
if (number == 0)
return 0;
else
return replace(number);
}
public static void main(String[] args) {
int number;
Scanner kb = new Scanner(System.in);
System.out.print("Enter the number : ");
number = kb.nextInt();
System.out.println("replace:"+replace(number));
int a, m = 0, sum = 0;
do{
a = replace(number) % 10;
m = m * 10 + a;
sum = sum + a;
number = replace(number) / 10;
}
while( replace(number) > 0);
System.out.println("Reverse:"+m);
System.out.println("Sum of digits:"+sum);
}
}
Currently the problem occurs in reversing the number because it also replace the last digit of the number, this should not happen.
> Input/Output of current program
>
> Enter the number : 2300
>
> replace:2301
>
> Reverse:1132
>
> Sum of digits:7
答案1
得分: 1
import java.util.*;
public class Main {
static int replace(int number){
if (number % 10 == 0)
return number + 1;
return number;
}
static int Convert(int number){
if (number == 0)
return 0;
else
return replace(number);
}
public static void main(String[] args) {
int number;
Scanner kb = new Scanner(System.in);
System.out.print("Enter the number: ");
number = kb.nextInt();
int a = 0, m = 0, sum = 0;
number = replace(number);
System.out.println("replace: " + number);
do{
a = number % 10;
m = m * 10 + a;
sum = sum + a;
number /= 10;
}
while( number > 0);
System.out.println("Reverse: " + m);
System.out.println("Sum of digits: " + sum);
}
}
Your code is fundamentally wrong because of the way you are replacing your numbers.
Changes made:
- Changed replacing algorithm (You cannot change all 0 values to 1 that is wrong and why you got the wrong values)
- Replace the number before you enter the loop. (You don't need to replace every iteration of the loop at 3 different places)
Expected output:
<details>
<summary>英文:</summary>
do this instead
import java.util.*;
public class Main {
static int replace(int number){
if (number %10 == 0)
return number += 1;
return number;
}
static int Convert(int number){
if (number == 0)
return 0;
else
return replace(number);
}
public static void main(String[] args) {
int number;
Scanner kb = new Scanner(System.in);
System.out.print("Enter the number : ");
number = kb.nextInt();
int a = 0, m = 0, sum = 0;
number = replace(number);
System.out.println("replace:" + number);
do{
a = number % 10;
m = m * 10 + a;
sum = sum + a;
number /= 10;
}
while( number > 0);
System.out.println("Reverse:"+m);
System.out.println("Sum of digits:"+sum);
}
}
Your code is fundamentally wrong because of the way you are replacing your numbers.
Changes made:
1. Changed replacing algorithm (You cannot change all 0 values to 1 that is wrong and why you got the wrong values)
2. Replace the number before you enter the loop. (You don't need to replace every iteration of the loop at 3 different place)
Expected output:
[![output][1]][1]
[1]: https://i.stack.imgur.com/CLmws.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论