- import java.util.Scanner;
- public class JPA03 {
- static Scanner keyboard = new Scanner(System.in);
- public static void main(String[] args) {
-
- int n;
- do
- {
- System.out.print("請輸入n的值(n>0 , 且為偶數):");
- n = keyboard.nextInt();
- if(n>0 && n%2==0)
- {
- int i = n/2;
- int count = (2+n)*i;
- int total = count/2;
- System.out.printf("2+4+...+%d=%d",n,total);
- break;
- }
- else if(n<=0 || n%2==1)
- {
- continue;
- }
- }while(true);
- }
- }
複製代碼 |