简体   繁体   English

Java:命令行输入的总和给出了array * answer的长度

[英]Java: Sum of command line inputs gives length of array*answer

I am inputing five or more variables on the command line and creating an array out of them. 我在命令行上输入五个或更多变量,并从中创建一个数组。 I'm then adding the values of these but I'm not sure why it's not working. 然后,我要添加这些值,但不确定为什么它不起作用。

My code is: 我的代码是:

public static void main(String args[]) {
    int t = args.length;
    int[] u;
    u = new int[t];
    int v = 0;
    for (int i=0; i<t; i++) {
        u[i]= Integer.parseInt(args[i]);
        for (int j=0; j<t; j++) {
            v = v + u[i];
            System.out.println(v);
        }
    }
}

However if I input 5 ones this outputs 25. 但是,如果我输入5,则输出25。

I'm not sure where I have gone wrong and any help would be greatly appreciated. 我不确定我哪里出错了,我们将不胜感激。

NOTE: My task requires I use no library functions. 注意:我的任务要求我不使用任何库函数。

1) You have a for loop inside another for loop that is causing the trouble. 1)您有一个for内部另一个循环for循环,是造成麻烦。

2) Also watch out for your variable naming --> use variable names that express/describe their purpose. 2)还要注意您的变量命名->使用表达/描述其目的的变量名称。 The variable name t is not too descriptive. 变量名称t不太具有描述性。

3) Also there is no need to create extra variables / arrays. 3)此外,也无需创建额外的变量/数组。 Just loop through the input and add them to the sum. 只需遍历输入并将它们加到总和中即可。

public static void main(String args[]) {
  int sum = 0;
  for (int i=0; i<args.length; i++) {
    sum  += Integer.parseInt(args[i]);
  }
  System.out.println(sum);
}

4) Thanks to Java 8 you can do a (bit complex) one liner: 4)借助Java 8,您可以执行(位复杂)一个线性处理:

public static void main(String args[]) {
    System.out.println(Arrays.stream(myarray).mapToInt(Integer::parseInt).sum());
}
  • Create a Stream of Strings ( Arrays.stream(args) ) 创建一个字符串流( Arrays.stream(args)
  • Convert these to IntStream ( mapToInt(Integer::parseInt) ) 将它们转换为IntStream( mapToInt(Integer::parseInt)
  • Get the Sum of the IntegerStream via sum() 通过sum()获取IntegerStream的sum()

Your inner for loop makes no sense and seems to be the cause of your problem. 您的内部for循环毫无意义,似乎是造成问题的原因。 Effectively you are doing t * t loops, which in your case is 5 * 5 which is why you get a result of 25 based on input of all 1's. 实际上,您正在执行t * t循环,在您的情况下为5 * 5 ,这就是为什么基于全1的输入得到25的结果的原因。

You only need to loop over the input array a single time, like this: 您只需要一次遍历输入数组,如下所示:

public static void main(String args[]) {
    int t = args.length;
    int[] u;
    u = new int[t];
    int v = 0;

    for (int i=0; i<t; i++) {
        u[i]= Integer.parseInt(args[i]);
        v = v + u[i];
    }

    System.out.println(v);
}

On a side note there is no reason to actually store an int array unless you are planning on using it later for something else. 附带一提,除非您打算稍后再将其用于其他用途,否则没有理由真正存储int数组。 Based on this specific requirement, it would be better written like so: 根据此特定要求,最好这样写:

public static void main(String args[]) {
    int length = args.length;
    int total = 0;

    for (int i = 0; i < length; i++) {
        int number = Integer.parseInt(args[i]);
        total += number;
    }

    System.out.println(total);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM