java - 将整数添加到乘法表

标签 java arrays

使用命令行命令:

java class -table 7

应该打印一个表格:

 7     7     7     7     7     7     7     7     7     7
 7     8     9    10    11    12    13    14    15    16
 7     9    11    13    15    17    19    21    23    25
 7    10    13    16    19    22    25    28    31    34
 7    11    15    19    23    27    31    35    39    43
 7    12    17    22    27    32    37    42    47    52
 7    13    19    25    31    37    43    49    55    61
 7    14    21    28    35    42    49    56    63    70
 7    15    23    31    39    47    55    63    71    79
 7    16    25    34    43    52    61    70    79    88

这是一个 10 x 10 乘法表,每个数字都加了“7”。 换句话说,它将下表中的每个数字加 7:

 0     0     0     0     0     0     0     0     0     0
 0     1     2     3     4     5     6     7     8     9
 0     2     4     6     8    10    12    14    16    18
 0     3     6     9    12    15    18    21    24    27
 0     4     8    12    16    20    24    28    32    36
 0     5    10    15    20    25    30    35    40    45
 0     6    12    18    24    30    36    42    48    54
 0     7    14    21    28    35    42    49    56    63
 0     8    16    24    32    40    48    56    64    72
 0     9    18    27    36    45    54    63    72    81

下面的代码是我目前所拥有的。它打印出从 0 开始的原始表。但是我不明白我应该如何将 args[1] 添加到每个值。 (args[1] 在上述情况下为 7)。如何将 args[1] 添加到表中的每个值?

如果用户在命令行中的 -table 之后没有准确地放置 1 个 int,我还应该打印“Argument count mismatch”。

对于我的代码,它会在适当的时间打印“Argument count mismatch”,但它会打印所有数字,所以它会打印该行 100 次。我知道我应该把那部分代码放在循环之外,因为我只希望它打印一次。我如何实现它才能做到这一点?

private static void table(String[] args) {
    int[][] table = new int[10][10];
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            if (args.length != 2)
                System.out.println("Argument count mismatch");
            else
                System.out.printf("%6d", i * j);  
        }
        System.out.println();
    }    
}

最佳答案

好的,我们将第二个命令行参数解析为 Int 并将其添加到每个值:

private static void table(String[] args) {
    int numToAdd = Integer.parseInt(args[1]);
    int[][] table = new int[10][10];
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            System.out.printf("%6d", i * j + numToAdd);  
        }
        System.out.println();
    } 
}

不确定为什么要创建表 double 组,因为您从未使用过它。

输出:

java Test -test 7
     7     7     7     7     7     7     7     7     7     7
     7     8     9    10    11    12    13    14    15    16
     7     9    11    13    15    17    19    21    23    25
     7    10    13    16    19    22    25    28    31    34
     7    11    15    19    23    27    31    35    39    43
     7    12    17    22    27    32    37    42    47    52
     7    13    19    25    31    37    43    49    55    61
     7    14    21    28    35    42    49    56    63    70
     7    15    23    31    39    47    55    63    71    79
     7    16    25    34    43    52    61    70    79    88

我会创建一个不同的方法:

public static boolean validateArgs(String[]args){
   //all code to validate the args here.  
   //print validation specific errors here
}

然后你的主要:

public static void main(String[]args){
    if(validateArgs(args)){
         table(args);
    }
}

关于java - 将整数添加到乘法表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26221243/

相关文章:

java - 当java使用getBoolean时,错误无法转换为内部表示 - JAVA,ORACLE

javascript - 将项目添加到 Observable Array Knockout

c++ - 数组大小 (new[])

php - array_key_exist() 以数组为键

java - 设置从 Java awt 窗口启动 .exe 外部应用程序的窗口位置

java - 免费/开源 Java 规则/工作流引擎

c - 将二维数组的程序拆分为函数 (C)

python - 访问数组的多个元素

java - Apache Camel 发送一条简单的消息

java - 如何仅为特定类配置 log4j2.xml 日志级别?