java 在一位数上加零,这可能吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11279683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:30:28  来源:igfitidea点击:

Adding zero to a single digit number, Is it possible?

java

提问by steve care

public class MultiplicationTable {
public static void main (String[]a){

    int[] x;
    x = new int[10];
    int i;
    int n=0;

    for (i=0;i<x.length;i++){
        n++;
        x[i]=n;
        System.out.print(x[i] + " ");
    }

    System.out.println();
    for (i=0;i<x.length;i++)            
        System.out.print(x[0] * x[i] + " ");

    System.out.println();
    for (i=0;i<x.length;i++)            
        System.out.print(x[1] * x[i] + " ");

    System.out.println();
    for (i=0;i<x.length;i++)            
        System.out.print(x[2] * x[i] + " ");

    System.out.println();
    for (i=0;i<x.length;i++)            
        System.out.print(x[3] * x[i] + " ");

    System.out.println();
    for (i=0;i<x.length;i++)            
        System.out.print(x[4] * x[i] + " ");

    System.out.println();
    for (i=0;i<x.length;i++)            
        System.out.print(x[5] * x[i] + " ");

    System.out.println();
    for (i=0;i<x.length;i++)            
        System.out.print(x[6] * x[i] + " ");

    System.out.println();
    for (i=0;i<x.length;i++)            
        System.out.print(x[7] * x[i] + " ");

    System.out.println();
    for (i=0;i<x.length;i++)            
        System.out.print(x[8] * x[i] + " ");

    System.out.println();
    for (i=0;i<x.length;i++)            
        System.out.print(x[9] * x[i] + " ");


}
}

This is a program that will display a Multiplication Table like this:

这是一个将显示乘法表的程序,如下所示:

1 2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10

2 4 6 8 10 12 14 16 18 20

2 4 6 8 10 12 14 16 18 20

3 6 9 12 15 18 21 24 27 30

3 6 9 12 15 18 21 24 27 30

4 8 12 16 20 24 28 32 36 40

4 8 12 16 20 24 28 32 36 40

5 10 15 20 25 30 35 40 45 50

5 10 15 20 25 30 35 40 45 50

6 12 18 24 30 36 42 48 54 60

6 12 18 24 30 36 42 48 54 60

7 14 21 28 35 42 49 56 63 70

7 14 21 28 35 42 49 56 63 70

8 16 24 32 40 48 56 64 72 80

8 16 24 32 40 48 56 64 72 80

9 18 27 36 45 54 63 72 81 90

9 18 27 36 45 54 63 72 81 90

10 20 30 40 50 60 70 80 90 100

10 20 30 40 50 60 70 80 90 100

===============================================================

================================================== ==============

It is running and correct but I want it to look like this one:

它正在运行且正确,但我希望它看起来像这样:

01 02 03 04 05 06 07 08 09 10

01 02 03 04 05 06 07 08 09 10

01 02 03 04 05 06 07 08 09 10

01 02 03 04 05 06 07 08 09 10

02 04 06 08 10 12 14 16 18 20

02 04 06 08 10 12 14 16 18 20

03 06 09 12 15 18 21 24 27 30

03 06 09 12 15 18 21 24 27 30

04 08 12 16 20 24 28 32 36 40

04 08 12 16 20 24 28 32 36 40

05 10 15 20 25 30 35 40 45 50

05 10 15 20 25 30 35 40 45 50

06 12 18 24 30 36 42 48 54 60

06 12 18 24 30 36 42 48 54 60

07 14 21 28 35 42 49 56 63 70

07 14 21 28 35 42 49 56 63 70

08 16 24 32 40 48 56 64 72 80

08 16 24 32 40 48 56 64 72 80

09 18 27 36 45 54 63 72 81 90

09 18 27 36 45 54 63 72 81 90

10 20 30 40 50 60 70 80 90 100

10 20 30 40 50 60 70 80 90 100

===============================================

================================================

Is there any way?

有什么办法吗?

回答by Steven Luu

Yes you can! You can use String.formatto add zero padding to your output.

是的你可以!您可以使用String.format向输出添加零填充。

Example:

例子:

String.format("%05d", 2)would produce 00002.

String.format("%05d", 2)会产生00002.

Some improvement on the current code:

对当前代码的一些改进:

I'm not sure why you intend to store the numbers inside an array (for practice purpose maybe), but that is not necessary as it goes from 1 to 10 anyway. Though if you want to do that, you don't need both iand n.

我不确定您为什么打算将数字存储在数组中(可能出于实践目的),但这不是必需的,因为它从 1 到 10 无论如何。虽然如果你想这样做,你不需要in

for (i=0; i<x.length; i++){
    x[i] = i+1;
    System.out.print(x[i] + " ");
}

Secondly, I'm sure you realize that you have a lot of duplicate code, and it's quite sequential. You can do that using 2 nested for loops, instead of having 10 single loops:

其次,我相信您已经意识到您有很多重复的代码,而且非常有顺序。您可以使用 2 个嵌套的 for 循环来做到这一点,而不是使用 10 个单循环:

for (int row = 1; row <= 10; row++) {
    for (int col = 1; col <= 10; col++)
        System.out.print(String.format("%03d", row * col));
    System.out.println();
}

回答by dcow

Yes:

是的:

String.format("%01d", x[i]*x[j]); is what you want.

If you're familiar with printfin C then this will be familiar. If not, read the java reference on String.format format strings.

如果您熟悉printfC,那么这将是熟悉的。如果没有,请阅读有关String.format 格式字符串的 java 参考。

Also, rather than 10 System.out.printlnstatements, you can use a doubly nested loop with two counters. One to count which row you're in jand one for each column i.

此外,System.out.println您可以使用带有两个计数器的双重嵌套循环,而不是 10 个语句。一个用于计算您所在的行j,每个列一个i