返回列表 發帖
  1. import java.util.Scanner;
  2. public class JPA03 {
  3.         static Scanner keyboard = new Scanner(System.in);
  4.     public static void main(String[] args) {
  5.             int sum=0;

  6.             do {
  7.                     System.out.println("請輸入n的值 ( n > 0 ,且為偶數)");
  8.                     int n = keyboard.nextInt();  
  9.                    
  10.                     if(n % 2 == 0&&n > 0){
  11.                             for(int i=2; i<=n; i+=2){
  12.                             sum+=i;
  13.                    
  14.                             }                           
  15.                             System.out.println("2+4+...+"+n+"="+sum);
  16.                             break;
  17.                     }else{
  18.                     System.out.println("請輸入n的值 ( n > 0 ,且為偶數)");
  19.                             n = keyboard.nextInt();
  20.                     }   
  21.             } while( true );
  22.     }
  23.         }
複製代碼

TOP

返回列表