3.10编程打卡

6.1(数学:五角数)

 1 import java.util.*;
 2 
 3 public class o {
 4     static int i=0;
 5     static int k=0;
 6     public static int getPentagonalNumber(int n) {
 7         k++;
 8         return i*(3*i-1)/2;
 9     }
10 
11 
12     public static void main(String[] args) {
13         Scanner input = new Scanner (System.in);
14         System.out.println("请输入五角数的数量:");
15         int n = input.nextInt();
16         for(;i<n;i++) {
17             System.out.print(getPentagonalNumber(n)+" ");
18          if(k%10==0) {
19              System.out.print("
");
20          }
21         }    
22     }
23 }
 

6.2*(求一个整数各位数字之和)

 1 import java.util.*;
 2 
 3 public class o {
 4 
 5     public static int sumDigits(long n) {
 6         long a = n / 100;
 7         long b = n % 100 /10;
 8         long c = n % 10;
 9         return (int) (a+b+c);
10 
11     }
12 
13     public static void main(String[] args) {
14         System.out.print("请输入一位三位数:");
15         Scanner input = new Scanner (System.in);
16         long n = input.nextLong();
17         while(n>999||n<100) {
18             n = input.nextLong();
19         }
20         System.out.println(sumDigits(n));
21     }
22 }

注:能力不足,只能输入一个三位数,求其各位数字之和

原文地址:https://www.cnblogs.com/ncoheart/p/8541407.html