Java 如何每隔';打印一个带逗号的整数;d';数字,从右到左
我必须编写一个程序,接收一个int'n'和另一个'd',并从右到左每隔d位打印数字n。 如果“n”或“d”为负数-程序将按原样打印“n”。 不过,我必须确保数字前后没有逗号,并且不允许使用字符串或数组 例如:n=12345678 d=1:1,2,3,4,5,6,7,8 d=3:12345678 我编写了以下代码:Java 如何每隔';打印一个带逗号的整数;d';数字,从右到左,java,loops,for-loop,Java,Loops,For Loop,我必须编写一个程序,接收一个int'n'和另一个'd',并从右到左每隔d位打印数字n。 如果“n”或“d”为负数-程序将按原样打印“n”。 不过,我必须确保数字前后没有逗号,并且不允许使用字符串或数组 例如:n=12345678 d=1:1,2,3,4,5,6,7,8 d=3:12345678 我编写了以下代码: public static void printWithComma(int n, int d) { if (n < 0 || d <= 0) { S
public static void printWithComma(int n, int d) {
if (n < 0 || d <= 0) {
System.out.println(n);
} else {
int reversedN = reverseNum(n), copyOfrereversedN = reversedN, counter = numberLength(n);
while (reversedN > 0) {
System.out.print(reversedN % 10);
reversedN /= 10;
counter--;
if (counter % d == 0 && reversedN != 0) {
System.out.print(",");
}
}
/*
* In a case which the received number will end with zeros, the reverse method
* will return the number without them. In that case the length of the reversed
* number and the length of the original number will be different - so this
* while loop will end the zero'z at the right place with the commas at the
* right place
*/
while (numberLength(copyOfrereversedN) != numberLength(n)) {
if (counter % d == 0) {
System.out.print(",");
}
System.out.print(0);
counter--;
copyOfrereversedN *= 10;
}
}
}
有人告诉我,代码并不适用于所有情况,我无法考虑这种情况(告诉我的人不会告诉我)
谢谢你的阅读 您的代码似乎过于复杂 如果您已经了解了递归,您可以这样做:
public static void printWithComma(int n, int d) {
printInternal(n, d, 1);
System.out.println();
}
private static void printInternal(int n, int d, int i) {
if (n > 9) {
printInternal(n / 10, d, i + 1);
if (i % d == 0)
System.out.print(',');
}
System.out.print(n % 10);
}
没有递归:
public static void printWithComma(int n, int d) {
int rev = 0, i = d - 1;
for (int num = n; num > 0 ; num /= 10, i++)
rev = rev * 10 + num % 10;
for (; i > d; rev /= 10, i--) {
System.out.print(rev % 10);
if (i % d == 0)
System.out.print(',');
}
System.out.println(rev);
}
您可以使用整个JavaAPI吗 使用
DecimalFormat
double in = 12345678;
DecimalFormat df = new DecimalFormat( ",##" );
System.out.println(df.format(in));
12,34,56,78
使用
=每组1个,#
=每组2个,##
=每组3个 等等,###
- 我花了好几分钟。下面的代码片段很好地完成了这项工作(下面的解释):
publicstaticvoidprintwithcomma(intn,intd){//n=number,d=commaIndex
最终整数长度=(int)(Math.log10(n)+1);//位数;
对于(inti=1;i0){//如果符合条件,请添加逗号
系统输出打印(“,”);
}
}
}
- 使用
我在整数中找到了一些数字((Math.log10(n)+1)
表示8
)12345678
- 循环的
确保了进一步计算所需的
系列(1、10、100、1000…)的指数。使用以10为底的对数,我得到循环的当前索引n
- 要得到第n个数字有点棘手,这个公式是基于。然后打印出来
- 最后,还要为逗号找到一个限定位置(
)。如果当前循环索引的模等于零,则达到dth索引并可以打印逗号。最后,条件,
可确保打印结果末尾不留逗号保持>0
您通过反转数字解决了数字循环问题,因此可以进行简单的十除法以按顺序接收所有数字 逗号位置是从右边开始计算的
public static void printWithComma(int n, int d) {
if (n < 0) {
System.out.print('-');
n = -n;
}
if (n == 0) {
System.out.print('0');
return;
}
int length = numberLength(n);
int reversed = reverseNum(n);
for (int i = 0; i < length; ++i) {
int nextDigit = reversed % 10;
System.out.print(nextDigit);
reversed /= 10;
int fromRight = length - 1 - i;
if (fromRight != 0 && fromRight % d == 0) {
System.out.print(',');
}
}
}
什么是
numberLength()
?如果n=0
那个代码确实很复杂,似乎什么也不打印,但我不允许使用字符串、数组或递归。我想,它不是。问题是“我不允许使用字符串或数组”“,##”
是一个字符串(文字),而format()
的返回值也是一个字符串。此外,输入是“一个int'n'和另一个int'd'”,那么在运行时如何从该输入中获取此代码?printWithComma(100000000,1)
导致无限循环。由于问题中的reverseEnum()
有缺陷,并且您正在使用它,因此您的代码有缺陷<代码>打印逗号(999999999,3)prints935600141
@andre由于这是一项家庭作业之类的任务,我没有为OP重写它,尽管我看到了那个黑客代码。OP提到已经有人告诉他有一个漏洞。用逗号简化打印应该有帮助。简化?你是说像?;-)@安德烈亚斯:是的,这是最好的解决方案,尽管我确实想关注一下OP的工作。。
double in = 12345678;
DecimalFormat df = new DecimalFormat( ",##" );
System.out.println(df.format(in));
public static void printWithComma(int n, int d) { // n=number, d=commaIndex
final int length = (int) (Math.log10(n) + 1); // number of digits;
for (int i = 1; i < Math.pow(10, length); i*=10) { // loop by digits
double current = Math.log10(i); // current loop
double remains = length - current - 1; // loops remaining
int digit = (int) ((n / Math.pow(10, remains)) % 10); // nth digit
System.out.print(digit); // print it
if (remains % d == 0 && remains > 0) { // add comma if qualified
System.out.print(",");
}
}
}
public static void printWithComma(int n, int d) {
if (n < 0) {
System.out.print('-');
n = -n;
}
if (n == 0) {
System.out.print('0');
return;
}
int length = numberLength(n);
int reversed = reverseNum(n);
for (int i = 0; i < length; ++i) {
int nextDigit = reversed % 10;
System.out.print(nextDigit);
reversed /= 10;
int fromRight = length - 1 - i;
if (fromRight != 0 && fromRight % d == 0) {
System.out.print(',');
}
}
}
public static void main(String[] args) {
for (int n : new int[] {0, 1, 8, 9, 10, 234,
1_234, 12_345, 123_456, 123_456_789, 1_234_567_890}) {
System.out.printf("%d : ", n);
printWithComma(n, 3);
System.out.println();
}
}