本帖最後由 駱顗安 於 2021-8-20 21:31 編輯
- import java.util.Scanner;
- public class JPA03 {
- static Scanner keyboard = new Scanner(System.in);
- public static void main(String[] args) {
- int n=0;
- do {
- System.out.print("請輸入 n 的值(n > 0,且為偶數):");
- n=keyboard.nextInt();
- } while (n%2!=0||n<=0);
- int t=2,sum=0;
- do {
- sum+=t;
- t+=2;
- } while (t<=n);
- System.out.println("2+4+...+"+n+"="+sum);
- }
- }
複製代碼 |