将所有 0 替换为 5 ||我写了代码,但它没有按预期工作| o/p倒序
Replace all 0's with 5 || i wrote code but its not working expected| o/p is in reverse order
问题:给定一个数字 N。任务是完成函数 convertFive(),用 5 替换数字中的所有零
我的代码|请验证任何帮助我
public class Replaceall0swith5 {
public static void convertFive(int n) {
//add code here.
int digit, sum = 0;
while (n > 0) {
digit = n % 10;
if (digit == 0) {
digit = 5;
}
sum = sum * 10 + digit;
n = n / 10;
}
System.out.print(n + " All O's replaced with 5 " + sum);
}
public static void main(String[] args) {
Replaceall0swith5.convertFive(1004);
}
}
您使用模数替换是否有原因?
public class Replaceall0swith5 {
public static void convertFive(int n) {
int replacedValues = Integer.valueOf(String.valueOf(n).replace("0","5"));
System.out.print(n + " All O's replaced with 5 " + replacedValues);
}
public static void main(String[] args) {
Replaceall0swith5.convertFive(1004);
}
}
除了将 0 变为 5 之外,这与反转数字基本相同。
while (n > 0) {
digit = n % 10;
if (digit == 0) {
digit = 5;
}
sum = sum * 10 + digit;
n = n / 10;
}
convertFive(1004)
给出 4551 而不是 1554。
数字 = 4 : 总和 = 0 * 10 + 4 = 4
数字 = 0 : 总和 = 4 * 10 + 5 = 45
数字 = 0 : 总和 = 45 * 10 + 5 = 455
数字 = 1 : 总和 = 455 * 10 + 1 = 4551
简单的解决方案是将其转换为字符串并将 0 替换为 5。@Fmxerz 已经给出了解决方案。
或者如果你打算这样做,你可以使用 Math.pow 函数。
import java.lang.Math;
....
public static void convertFive(int n) {
int i = 0;
int digit, sum = 0;
while (n > 0) {
digit = n % 10;
if (digit == 0) {
digit = 5;
}
sum = sum + digit*(int)Math.pow(10,i);
n = n / 10;
i++;
}
System.out.print(n + " All O's replaced with 5 " + sum);
}
试试这个。
public static void convertFive(int n) {
int sum = 0;
for (int rank = 1; n > 0; n = n / 10, rank *= 10) {
int digit = n % 10;
if (digit == 0)
digit = 5;
sum = sum + digit * rank;
}
System.out.print(n + " All O's replaced with 5 " + sum);
}
n
digit
rank
sum
1004
1
0
100
4
1
4
10
0 -> 5
10
54
1
0 -> 5
100
554
0
1
1000
1554
问题:给定一个数字 N。任务是完成函数 convertFive(),用 5 替换数字中的所有零 我的代码|请验证任何帮助我
public class Replaceall0swith5 {
public static void convertFive(int n) {
//add code here.
int digit, sum = 0;
while (n > 0) {
digit = n % 10;
if (digit == 0) {
digit = 5;
}
sum = sum * 10 + digit;
n = n / 10;
}
System.out.print(n + " All O's replaced with 5 " + sum);
}
public static void main(String[] args) {
Replaceall0swith5.convertFive(1004);
}
}
您使用模数替换是否有原因?
public class Replaceall0swith5 {
public static void convertFive(int n) {
int replacedValues = Integer.valueOf(String.valueOf(n).replace("0","5"));
System.out.print(n + " All O's replaced with 5 " + replacedValues);
}
public static void main(String[] args) {
Replaceall0swith5.convertFive(1004);
}
}
除了将 0 变为 5 之外,这与反转数字基本相同。
while (n > 0) {
digit = n % 10;
if (digit == 0) {
digit = 5;
}
sum = sum * 10 + digit;
n = n / 10;
}
convertFive(1004)
给出 4551 而不是 1554。
数字 = 4 : 总和 = 0 * 10 + 4 = 4
数字 = 0 : 总和 = 4 * 10 + 5 = 45
数字 = 0 : 总和 = 45 * 10 + 5 = 455
数字 = 1 : 总和 = 455 * 10 + 1 = 4551
简单的解决方案是将其转换为字符串并将 0 替换为 5。@Fmxerz 已经给出了解决方案。
或者如果你打算这样做,你可以使用 Math.pow 函数。
import java.lang.Math;
....
public static void convertFive(int n) {
int i = 0;
int digit, sum = 0;
while (n > 0) {
digit = n % 10;
if (digit == 0) {
digit = 5;
}
sum = sum + digit*(int)Math.pow(10,i);
n = n / 10;
i++;
}
System.out.print(n + " All O's replaced with 5 " + sum);
}
试试这个。
public static void convertFive(int n) {
int sum = 0;
for (int rank = 1; n > 0; n = n / 10, rank *= 10) {
int digit = n % 10;
if (digit == 0)
digit = 5;
sum = sum + digit * rank;
}
System.out.print(n + " All O's replaced with 5 " + sum);
}
n | digit | rank | sum |
---|---|---|---|
1004 | 1 | 0 | |
100 | 4 | 1 | 4 |
10 | 0 -> 5 | 10 | 54 |
1 | 0 -> 5 | 100 | 554 |
0 | 1 | 1000 | 1554 |