1. 程式人生 > >3.10編程打卡

3.10編程打卡

color tint mil div 3.1 digi post 數學 public

6.1(數學:五角數)

import java.util.*;

public class o {

static int i=0;

static int k=0;

public static int getPentagonalNumber(int n) {

k++;

return i*(3*i-1)/2;

}

public static void main(String[] args) {

Scanner input = new Scanner (System.in);

System.out.println("請輸入五角數的數量:");

int n = input.nextInt();

for(;i<n;i++) {

System.out.print(getPentagonalNumber(n)+" ");

if(k%10==0) {

System.out.print("\n");

}

}

}

}

6.2*(求一個整數各位數字之和)

import java.util.*;

public class o {

public static int sumDigits(long n) {

long a = n / 100;

long b = n % 100 /10;

long c = n % 10;

return (int) (a+b+c);

}

public static void main(String[] args) {

System.out.print("請輸入一位三位數:");

Scanner input = new Scanner (System.in);

long n = input.nextLong();

while(n>999||n<100) {

n = input.nextLong();

}

System.out.println(sumDigits(n));

}

}

註:能力不足,只能輸入一個三位數,求其各位數字之和

3.10編程打卡