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