生成逗号分隔值和和的Java代码

生成逗号分隔值和和的Java代码,java,indexoutofboundsexception,Java,Indexoutofboundsexception,运行此代码时,我获得了ArrayIndexOutOfBoundsException public class Evensum { public static void main(String[] args) { int num = Integer.parseInt(args[0]); int even[] = new int[num]; int sum = 0,j = 0; String evennums = "";

运行此代码时,我获得了
ArrayIndexOutOfBoundsException

public class Evensum {

    public static void main(String[] args) {
        int num = Integer.parseInt(args[0]);
        int even[] = new int[num];
        int sum = 0,j = 0;
        String evennums = "";
        //Insert your code here
         for(j=0; j<=num; j++) {
            if(num%2==0) {
                even[j]=num;
                sum=sum+num;
                args[j]= Integer.toString(num);
            }
            evennums=String.join(",", args);
        }    
        System.out.println(evennums);
        System.out.println(sum);
    }
}
公共类Evensum{
公共静态void main(字符串[]args){
int num=Integer.parseInt(args[0]);
整数偶数[]=新整数[num];
整数和=0,j=0;
字符串evennums=“”;
//在这里插入您的代码
对于(j=0;j
此行将引发另一个异常。我假设您仅从命令行传递一个参数,即
args[0]
。这意味着
args
数组的大小为1,您无法向其添加更多元素

此外,向
args
数组添加/修改元素不是一个好的做法。您应该为此创建一个新数组。

替换

for (j = 0; j <= num; j++)

对于(j=0;j请避免使用
Integer.toString
String.join
来传递参数,从而找到更简单的代码版本。简单Integer
Arraylist
并添加元素即可

package com.umapathy.java.learning.programs;
import java.util.ArrayList;
import java.util.List;
public class EvenSum 
{
    public static void main(String[] args)
{   
    int num = 20; //Initialize the user-defined value for the loop execution
    int sum = 0 ; //Initialize the Sum Value as 0
    List<Integer> evenlist = new ArrayList<Integer>(); //Define an integer 
    Array list
    for (int i=2; i<=num; i++) //Begin the loop from the value of 2 
    {
      if(i%2==0) //Logic to find whether a given number is Even Number or not
      {
      sum = sum + i;  // If the logic returns true, Calculate the Sum Value 
      evenlist.add(i); // Add the Integer to the previous defined Integer 
                       // Arraylist by calling add method
       }        
    }
    System.out.println(evenlist); // Print the Output outside the loops
    System.out.println(sum);
     }
  }
包javaApp;
公务舱{
公共静态void main(字符串[]args){
int num=20;
整数偶数[]=新整数[num];
整数和=0,j=0;
字符串evennums=“”;

对于(j=1;j如果初始时实际长度未知,为什么要初始化偶数数组大小。在这种情况下,最好使用具有动态增长特性的ArrayList

用这种方法试试看

    public static void main(String[] args) {
            int num =  Integer.parseInt(args[0]);
            List<Integer> evens = new ArrayList<Integer>();
            int sum = 0;                
            for (int j = 0; j <= num; j++) {
                if (j % 2 == 0) {
                    sum += j;
                    evens.add(j);
                }
            }       
            System.out.println("Even Numbers List : "+evens);
            System.out.println("Total Sum : "+sum);
      

     // If you want an array of int instead of ArrayList you can convert ArrayList into int[] with following two lines

           int evenArray[] = even.stream().mapToInt(x->x).toArray();                
           System.out.println("Even Numbers Array : "+evenArray);

        }
publicstaticvoidmain(字符串[]args){
int num=Integer.parseInt(args[0]);
List evens=new ArrayList();
整数和=0;
对于(int j=0;j x).toArray();
System.out.println(“偶数数组:“+evenArray”);
}
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
//System.out.println(“Args:+Args[0]);
int num=Integer.parseInt(args[0]);
整数偶数[]=新整数[num];
整数和=0,j=0;
字符串evennums=“”;
//在这里插入您的代码
对于(j=0;j运行方式-->运行配置
转到“参数”选项卡,并将值传递为10
单击运行
输出:
2,4,6,8,10

30

Try
jhi您的代码运行了吗?@theearner360-如果其中一个答案解决了您的问题,您可以通过将其标记为已接受来帮助社区。已接受的答案可以帮助未来的访问者自信地使用该解决方案。检查以了解如何操作。对代码示例的解释可能有助于提问者下注请理解这将如何解决问题。
for (j = 0; j <= num; j++)
for (j = 0; j < num; j++)
package com.umapathy.java.learning.programs;
import java.util.ArrayList;
import java.util.List;
public class EvenSum 
{
    public static void main(String[] args)
{   
    int num = 20; //Initialize the user-defined value for the loop execution
    int sum = 0 ; //Initialize the Sum Value as 0
    List<Integer> evenlist = new ArrayList<Integer>(); //Define an integer 
    Array list
    for (int i=2; i<=num; i++) //Begin the loop from the value of 2 
    {
      if(i%2==0) //Logic to find whether a given number is Even Number or not
      {
      sum = sum + i;  // If the logic returns true, Calculate the Sum Value 
      evenlist.add(i); // Add the Integer to the previous defined Integer 
                       // Arraylist by calling add method
       }        
    }
    System.out.println(evenlist); // Print the Output outside the loops
    System.out.println(sum);
     }
  }
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20] 110
package javaApp;

public class EvenSum {

    public static void main(String[] args) {
        int num = 20;
        int even[] = new int[num];
        int sum = 0,j = 0;
        String evennums = "";
        for(j=1; j<=num; j++) {
            if(j%2==0) {
                sum=sum+j;
                evennums=evennums+","+j;
            }
        }
        evennums=evennums.replaceFirst(",","");
        System.out.println(evennums);
        System.out.println(sum);
    }
}
    public static void main(String[] args) {
            int num =  Integer.parseInt(args[0]);
            List<Integer> evens = new ArrayList<Integer>();
            int sum = 0;                
            for (int j = 0; j <= num; j++) {
                if (j % 2 == 0) {
                    sum += j;
                    evens.add(j);
                }
            }       
            System.out.println("Even Numbers List : "+evens);
            System.out.println("Total Sum : "+sum);
      

     // If you want an array of int instead of ArrayList you can convert ArrayList into int[] with following two lines

           int evenArray[] = even.stream().mapToInt(x->x).toArray();                
           System.out.println("Even Numbers Array : "+evenArray);

        }
public static void main(String[] args) {
        // TODO Auto-generated method stub
        //System.out.println("Args: "+args[0]);
        int num = Integer.parseInt(args[0]);
        int even[] = new int[num];
        int sum = 0,j = 0;
        String evennums = "";
        //Insert your code here
        for(j=0; j<=num; j++) {
            if(j%2==0) {
                //even[j]=num;
                sum=sum+j;
                if(j!=0) evennums=evennums+","+j;

            }

        }    
        evennums=evennums.substring(1);
        System.out.println(evennums);
        System.out.println(sum);
    }