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)); } } |
注:能力不足,只能输入一个三位数,求其各位数字之和