package Test;
import java.util.Scanner;
public class Test5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("输入一个n:");
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();
int Sum=sum(i);
System.out.println(Sum);
}
public static int sum(int n)
{
int Sum=0;
for(int j=1;j<=n;j++)
{
if(j%2==0||j%3==0)
Sum=Sum+j;
}
return Sum;
}
}
代码结果: