Today’s sharing started, please give us more advice ~

Java code implementation is more basic content, today to introduce you to a few topics training, you can operate to achieve oh!

1. Output age according to age value

According to their age, they are young (below 18), young (19-28), middle (29-55) and old (above 56).

Java code implementation

Compilation effect:

We enter the size of a value, and the program prints out the corresponding age group.

Print 1-100 prime numbers

Print primes between 1 and 100

Java code implementation

Compilation effect:

Note: To print primes between 1 and 100, we use the simpler method, which can be solved more easily, such as I ++ –> I +=2. Even numbers cannot be prime numbers. I can also narrow j down to I over 2 or I squared.

Judge prime numbers

Enter a number to determine whether it is prime

Java code implementation:

Compilation effect:

Note: Same as above

Output leap year

Output all leap years between 1000 and 2000

Java code implementation:

Compilation effect:

Output multiplication table

Output the 9*9 multiplication table

Java code implementation:

Compilation effect:

Note: To format the output, we can use the format of printf function in C language.

Find the greatest common divisor of two positive integers

Input two positive integers and output their greatest common divisor

Java code implementation:

Compilation effect:

Note: we did toss and turn division

Evaluate the expression

Calculate 1/1-1/2+1/3-1/4+1/5…… Plus 1/99 minus 1/100.

Java code implementation:

Compilation effect:

Sum += (flag) * (1.0 / I); 1 / I, not 1 / I. Sum is double.

The number of occurrences of the number 9

Write a program to count the number 9 in all integers from 1 to 100

Compilation effect:

Note: there are two occurrences of 9 in 99, so we use two if statements to count the number of 9 in each number.

Nine, the number of narcissus

Topic content:

Find all daffodil numbers between 0 and 999 and print them.

“Daffodil number” is a three-digit number whose cube sum is exactly equal to the number itself, such as :153 = 1+5+3? , then 153 is a “daffodil number.

Java code implementation:

public static void main(String[] args) { for(int n=1; n<999; n++){ int tmp=n; int count = 0; //1. Calculate the number of digits while(TMP! =0){ count++; tmp = tmp/10; }// Count is the number of digits //2. int sum = 0; while(tmp! =0){ sum += Math.pow(tmp%10,count); tmp = tmp/10; If (sum==n){system.out.println (sum+" is the number of daffodils "); }}}Copy the code

Compilation effect:

Thinking steps:

1. Figure out how many digits there are in the number

2. Take each digit of the number and find the sum of each digit to the power

3. Is the comparison result equal to the original number

Write code to simulate the scenario of three password inputs

Topic content:

You can enter the password for a maximum of three times. If the password is correct, the message “Login succeeded” is displayed. If the password is incorrect, you can enter the password again.

Enter a maximum of three times. If three times are wrong, it prompts you to exit the program

Java code implementation:

Compilation effect:

Note: equals() compares strings.

Find the number of 1’s in binary bits

Write a function that returns the number of 1’s in the binary. For example: 15 0000 1111 4 1’s

Compilation effect:

Note: The ampersand operator is used to specify that each bit of a binary ampersand can be used to obtain the digit

Find binary parity sequence

Get all the even and odd bits in a binary sequence of numbers and output the binary sequence respectively.

Java implementation code:

Compilation effect:

Note: this is an extension of Exercise 12, where we take bits based on the odd and even bits of a binary sequence.

Number guessing games

Title content: We implement a simple guess number game, the computer randomly generated within 100 numbers, we guess, until guess right, the program quit.

Java code implementation:

Compilation effect:

Well, our Java basic topic sharing ended here, I hope you practice a lot.

Summary:

: actually, I want to say, Java based sincerity is very important, consolidate the foundation is the key and core, great oaks grow from little acorns, no stable foundation and repairing buildings final result also will collapse, so the work is great improve the technology, research the basic technical design thoughts and intentions, don’t be hard at work to do a “doer”!

Today’s share has ended, please forgive and give advice!