This chapter introduces the basic concept of control statements. By learning the basic concepts of control sentences, you can finally achieve programming for the millionaires and golden section code.
- If
If (expression one) {
expression two;
}
If the value of expression 1 is true,
Execute expression 2.
- Multiple expressions and one expression
Under what circumstances are one expression and multiple expressions executed?
The specific code example is as follows:
public class essay {
public static void main(String[] args) {
boolean b = false;
//If there are multiple expressions, they must be enclosed in curly braces.
if(b){
System.out.println(“yes1”);
System.out.println(“yes2”);
System.out.println(“yes3”);
}
//Otherwise the expression 2 and expression 3 will be executed regardless of whether b is true.
if(b)
System.out.println(“yes1”);
System.out.println(“yes2”);
System.out.println(“yes3”);
//If there is only one expression, there is no need to write parentheses, it looks simpler than multiple expressions.
if(b){
System.out.println(“yes1”);
}
if(b)
System.out.println(“yes1”);
}
}

- Errors and solutions during the use of if

In line 14, there is a semicolon after the if, and the semicolon is also a complete expression.
If b is true, this semicolon will be executed, and then yes1 will be printed.
If b is false, this semicolon will not be executed, and then yes1 will be printed.
So, keep in mind that there cannot be a semicolon after if, otherwise it may cause ambiguity.
- The difference between if else and else if
The else in if else represents the case of failure.
The specific code is as follows:
public class essay {
public static void main(String[] args) {
boolean b = false;
if(b)
System.out.println(“yes1”);
else
System.out.println(“no”);
}
}

The else if is defined as multi-condition judgment
The specific code is as follows:
public class essay {
public static void main(String[] args) {
int a=1;
if(a==1)
System.out.println(“yes1”);
else if(a==2)
System.out.println(“yes2”);
else if(a==3)
System.out.println(“yes3”);
}
}

- Switch
The switch statement is equivalent to another expression of if else.
- The scope of application of switch
Switch can be used when the variable type is byte, short, int, char, String.
【note】At the end of each expression, use the break statement to end, otherwise the process controlled by switch will not end.
The specific code example is as follows:
public class essay {
public static void main(String[] args) {
int a=1;
switch(a)
{ case 1:
System.out.println(“yes1”);
break;
case 2:
System.out.println(“yes2”);
case 3:
System.out.println(“yes3”);
default:
System.out.println(“what?”);
}

- The difference between while and do-while loop statements
- While
As long as the expression in the while holds, it will continue to loop。
The specific code is as follows:
public class essay {
public static void main(String[] args) {
//Print 1 to 4
int i = 0;
while(i<5){
System.out.println(i);
i++;
}
}
}

- Do while
The difference with while is that no matter whether the condition is established or not, it is executed once and then judged.
The specific code is as follows:
public class essay {
public static void main(String[] args) {
int i = 0;
do{
System.out.println(i);
i++;
} while(i<5);
}
}

- Continue
continue means to continue the next cycle. So, when do we use continue?
The specific code is as follows:
public class essay {
public static void main(String[] args) {
//Print even
for (int j = 0; j < 10; j++) {
if(0==j%2)
continue; //If it is a double number, the following code will not be executed, and the next cycle will proceed directly
System.out.println(j);
}
}
}

- Exercise1-Millionaire
Assuming that your monthly income is 6000, except for usual expenses, you leave 2000 yuan each month for investment.
Then you carefully studied the investment methods of stocks and funds and achieved an annual investment return rate of 20%.
So, the question is, at the pace of investing 2,000 yuan per month, how many years of continuous investment, the total income reaches one million.
(Compound interest calculation is calculated based on 24,000 investment per year, not monthly interest calculation)
The specific code is as follows:
public class essay {
public static void main(String[] args) {
int fundPerMonth = 2000;
int fundPerYear = fundPerMonth *12;
double rate = 0.20;
//F = p* ( (1+r)^n );
int sum = 0;
int target = 1000*1000;
for (int j = 1; j < 100; j++) {
int year = j;
double compoundInterestRate = 1;
for (int i = 0; i < year; i++) {
compoundInterestRate = compoundInterestRate * (1+rate);
}
int compoundInterest = (int) (fundPerYear * compoundInterestRate);
sum +=compoundInterest;
System.out.println(“after” + year + ” year, total revenue ” + sum);
if(sum>=target){
System.out.println(“Total need” + year + “year,cumulative income exceeds “+ target );
break;
}
}
}

The final console output:

It can be seen that it takes a total of 12 years to become a millionaire.
Exercise2-Golden Ratio Point
Find two numbers to divide, and the result is closest to the golden section point 0.618.
The denominator and numerator cannot be even at the same time.
The denominator and numerator are in the range of [1-30].
The specific code is as follows:
public class essay {
public static void main(String[] args) {
int range = 30; // Range of values
float breakPoint = 0.618f; // Golden Ratio Point
float minDiff = 100; // The difference from the golden section point
int answerMolecule = 0; // Molecule found
int answerDenominator= 0; // Denominator found
for (int Molecule = 1; Molecule <= range;Molecule++) {
for (int Denominator = 1; Denominator <= range;Denominator++) {
//The denominator and numerator cannot be even at the same time
if (Molecule % 2==0 && Denominator%2==0)
continue;
// Value
float value = (float)Molecule / Denominator;
// Take the difference from the golden section point
float diff = value – breakPoint;
// Absolute value
diff = diff < 0 ? 0 – diff : diff;
// Find the smallest difference
if (diff < minDiff) {
minDiff = diff;
answerMolecule=Molecule;
answerDenominator=Denominator;
}
}
}
System.out.println(“From the golden section(” + breakPoint + “)The nearest two numbers are divided by:” + answerMolecule + “/” + answerDenominator + “=”
+ ((float) answerMolecule / answerDenominator));
}
}

The final console output:

Today’s tutorial is over, I hope people who watch this tutorial can do it by themselves and type the code line by line. By studying the content of this chapter, you can infer when you become a millionaire, which is very interesting.
I read this article fully on the topic of the difference of most recent and preceding technologies, it’s amazing
article.
I really like what you guys are up too. This type of clever work and coverage!
Keep up the amazing works guys I’ve incorporated
you guys to our blogroll.