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