返回列表 發帖
本帖最後由 駱顗安 於 2021-8-20 21:31 編輯
  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 n=0;
  6.         do {
  7.                         System.out.print("請輸入 n 的值(n > 0,且為偶數):");
  8.                         n=keyboard.nextInt();
  9.                 } while (n%2!=0||n<=0);
  10.         int t=2,sum=0;
  11.         do {
  12.                         sum+=t;
  13.                         t+=2;
  14.                 } while (t<=n);
  15.         System.out.println("2+4+...+"+n+"="+sum);
  16.     }
  17. }
複製代碼

TOP

返回列表