1.//Output yesterday's time at this moment
Calendar cal = Calendar.getInstance(); System.out.println(Calendar.DATE);//5 cal.add(Calendar.DATE, -1); Date time = cal.getTime(); System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(time));
2.//Print out the numbers in a string
String str = "iu7i8hy4jnb2"; Pattern p = Pattern.compile( "\\d"); Matches matches = p.matches (str); while ( matcher.find() ) { System.out.print(matcher.group());//7842 }
3.//Determine whether a year is a leap year
A year that is divisible by 4 but not divisible by 100 or a year that is divisible by 400 is called a leap year.
public static void leapYear(){ Scanner in = new Scanner(System.in); System.out.println("Please enter the year: "); while(in.hasNext()){ int year = in.nextInt(); if((year % 4 == 0 && year % 100 != 0) || year%400 == 0){ System.out.println(year + "Year is a leap year!"); }else{ System.out.println(year + "Year is not a leap year!"); } } }
4.1 // factorial
When expressing factorial, use "!" to indicate. For example, the factorial of n is expressed as n!
public static void factorial(int num){ int n = 1; for (int i = 1; i <= num; i++) { n = n * i; } System.out.println(n); }
4.2 // factorial summation
output 1! +2! +3! +…+num! the sum
public void factorial(int num){ int sum = 0; int n = 1; for (int i = 1; i <= num; i++) { n = n * i; sum = sum + n; } System.out.println("sum:" + sum); }
5.//Recursive algorithm - Fibonacci sequence problem - give birth to a litter of rabbits every three months
public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Please enter the number of months: "); while(in.hasNext()){ int month = in.nextInt(); System.out.println("The total number of rabbits in "+month+ "month is: " + rabbit(month)); } }//main public static int rabbit(int months){ if(months < 3){ return 1; }else{ return rabbit(months - 1) + rabbit(months - 2); } }
6.//Judgment and output the number of prime numbers
A prime number, also called a prime number, is a number that can only be rounded off by 1 and itself
// num Prime number public static void prime(int num){ int count = 0; for(int i = 2;i<= num;i++){ if(isSuShu(i)){ System.out.print(i + " "); count ++ ; } } System.out.println(); System.out.println("Total" + count + "Primes"); } // Determine if it is a prime number public static boolean isSuShu(int num){ for (int i = 2; i < num; i++) { if(num % i == 0){ return false; } } return true; }
7.//Print out the number of all daffodils
The so-called "daffodil number" refers to a three-digit number whose cubic sum of the digits is equal to the number itself. For example: 153 is a "daffodils number" because 153=1*1*1+5*5*5+3*3*3. (153, 370, 371, 407)
public static void daffodilsNumber(){ int a,b,c,sum; for (int i = 100; i < 1000; i++) { a = i / 100;//hundreds b = (i - 100*a) / 10; c = i % 10;//units sum = a*a*a + b*b*b + c*c*c; if(sum == i){ System.out.println(i); } } }
8.//Enter a positive integer factor. For example: input 90, output 90=2*3*3*5
1. Use a for loop to traverse a number starting from k=2 to find k<=n
2. When n%k==0, output the value of k
3. Then recurse the value of n, that is n=n/k;
4. At this time, the for loop should be re-executed, that is, k=2;
public static void num(){ Scanner sc = new Scanner(System.in); System.out.println("Please enter an integer: "); int n = sc.nextInt(); System.out.println(n + "="); for (int i = 2; i <= n/2; i++) { if(n % i ==0){ System.out.println(i + "*"); n =n /i; i = 2; } } System.out.println(n); }
8.//Conditional operator
Students with academic scores >=90 points are represented by A, those with scores between 60 and 89 are represented by B, and those with scores below 60 are represented by C. One of the knowledge points involved here is the conditional operator.
public static void core(){ System.out.print("Please enter an integer score: "); Scanner sc = new Scanner(System.in); char s; while(sc.hasNext()){ int core = sc.nextInt(); s = core>=90?'A' : core>=60?'B':'C'; System.out.println(s); } }