1. Huang Rong's math problems
Those who have read the legend of the Condor Heroes must be deeply touched by Huang Rong's clever ghosts, ice and snow. For example, when Huang Rong met the god operator's aunt, one of the three questions she had given her had the following question: Today there are things that do not know the number of things, there are two of three, and three of the five are left, seven or seven are the only two left? That is to say, there is an unknown number. This number is divided by three more than two, divided by five more three, divided by seven more than two. What is this number? He cleverly embedded the essence of Ancient Chinese Mathematics in a martial arts masterpiece, and Mr. Jin was a great hero!
Tips
Get started with Eclipse 3 Release
After a long test iteration of almost appetizing n, Eclipse.org finally released the official version on time. For Java enthusiasts, It is a grand summer gift.
Eclipse 3 has many new features, adding many considerate details. Read Eclipse 3.0 New and Noteworthy: workshop!
Ii. Java v. s. God Operator
Unfortunately, there is no Java in Song Dynasty, otherwise yinggu will not be overwhelmed by Huang Rong.
Open Eclipse and create a Java project named "Huang Rong's question ". Create a new Java class named javac1c. Remember to check it before the "public static void main (String [] args)" option. Enter the Java program as shown in figure 1, with a total of five lines of code (figure 1 ).
Run the program and you will get the result 2 (figure 2 ).
Through checking, 23 is indeed a solution to satisfy the question. I will not repeat the specific procedure of programming. If you are not familiar with it, please review the previous Java coffee!
In just a short time, the five-line program will solve the problem that I haven't figured out when I scratched my scalp. Is it amazing? In fact, these codes contain many Java language features, such as variables, operators, and flow control statements. Maybe these codes are still a bit mysterious to you. When combined with this question, let's take a look at the following introduction, and I will soon understand the mysteries.
1. variable)
Variables are objects represented by identifiers (with names) used to store values (with connotations. Code:
Int X;
Is a variable declaration statement, declaring X as an int type variable.
To declare a variable, you must specify the type and name of the variable.
The variable type is used to determine the data types that can be stored and the operations that can be performed on the variable. For example, in the code above, X is an integer of the int type and can only be assigned to this variable, for example, X = 0. You can only perform arithmetic operations on X, such as addition, subtraction, multiplication, division, and remainder.
Java variables are classified into "primitive type" and "reference type. The "Original Type" variable stores simple data of a specific size and type, such as an integer, a character, and a Boolean value (the variable type named by the logologist Geogre Boole, there are only two types of values-True or false, which belong to the classic binary logic. Unlike "Original Type", "reference type" is a reference concept that points to a specific object (Address) in the memory (see figure 3) (In Figure 3 + 4, the left side is "original type" and the right side is "reference type". We can see their differences through comparison ).
We now have a lot of contact with the "original type", which is sufficient to complete complex tasks. "Reference Type" plays an important role in the object-oriented concept.
Why are there two types of variables in Java? This is out of consideration for execution efficiency. The "reference type" serves objects and is an object-oriented infrastructure. The architecture and design system of the object-oriented method are very beautiful, however, the execution efficiency of the "reference type" is far less than that of the "original type ". Therefore, Java is actually a computer language used in combination with the "original type" and "reference type". From this point of view, Java is not a pure object-oriented language. Of course, Java provides the Integer (reference type) Java class that echo the int type (original type). They can be converted to each other. The new version of Java also provides the automatic conversion function, this is not a reference.
The original Java types are listed as follows:
| Type |
Description |
Size |
| Byte |
8-bit long integer |
8-bit |
| Short |
Short integer |
16-bit |
| Int |
Integer |
32-bit |
| Long |
Long Integer |
64-bit |
| Float |
Single-precision floating point number |
32-bit IEEE 754 |
| Double |
Double-precision floating point number |
64-bit IEEE 754 |
| Char |
Single Character |
16-bit Unicode characters |
| Boolean |
Boolean Value |
True or false |
It is worth mentioning that the Java language clearly defines the size of the variable type, paving the way for cross-platform.
The variable name is the name of the variable. you access the actual variable through the variable name, so there are strict rules on the variable name. In Java, the variable name must be:
★An identifier, that is, a string starting with a symbol.
★It cannot be a keyword, a Boolean value (true or false), or a reserved word null.
★The variable name must be unique within the scope.
It seems a lot of rules, but it is also reasonable. For example, if you give a child a name with a hundred surnames as the last name, if the name is "¥ Hao", others must think it is a joke or a strange network name. Of course, the name of a child cannot be a keyword such as "President" or "Emperor" to avoid misunderstanding. Speaking of the unique scope, it is also easy to understand: there are too many people in China called Wu zongxian, and the school (One Scope) will use the student ID to uniquely identify you, instead of forcing you to change your name to Wu zongxian 2004.
Although valid names can all be variable names, it is a good quality to give a meaningful name to the variable, so that no one else can read the code and cannot understand it. For example, if you give a variable name a dark sign, you 'd better not give it a dark sign like "killing me or not". Otherwise, you may feel better ......
Finally, let's talk about the scope. Scope refers to the area where variables can be accessed within the program. For example, in our program, variable X can be accessed in the entire main method, which is very intuitive. You can use the power of Eclipse to experience this concept: If the scope is not correct, Eclipse will not hesitate to warn you.
When a variable is defined, it can be assigned a value through "=", just as it is done in the program.
2. Operators
Operators are used for function operations. Operators in Java can be divided into Arithmetic Operators, Relational operators, conditional operators, displacement and logical operators, value assignment operators, and other operators.
Next, we use "(X % 3 = 2) & (X % 5 = 3) & (X % 7 = 2)" to understand the concept of Java operators.
As we have already said, "X" is a variable, and "%" is an arithmetic operator for modulo operation. "X % 3" is X modulo 3, which is the remainder of X divided by 3; "=" is a relational operator. If "=" is equal to the two sides, true is returned. Otherwise, false is returned. Therefore, the meaning of "X % 3 = 2" is quite clear-if the remainder of X divided by 3 is 2, the value of this expression is true; otherwise, it is false. A pair of "()" is used to indicate the priority of the operation. That is, the expressions in the brackets are calculated first, and then those outside the brackets. "&" Is a condition operator and a union in a logical operation. That is, if and only when both sides of "&" are true, the expression is true, otherwise, the value is false. Now let's look at the above expression: X divided by 3 and 2 and X divided by 5 and X divided by 7 and 2, the expression result is obviously a Boolean value, either true or false.
3. Process Control
Programs are stored on disks as files. If there is no process control, the Java interpreter executes the statements from top to bottom and from left to right. With flow control statements, you can selectively execute some statements, repeat some statements, or change the sequence of statements to complete the ever-changing work.
First, we will introduce the if flow control statements. The if statement allows you to execute some statements selectively, which is the same as "what if so. For example, our program is written as follows:
If (X % 3 = 2) & (X % 5 = 3) & (X % 7 = 2 )){
System. out. print ("the number is :");
System. out. print (X );
}
It is obvious that, in the preceding example, if "X divided by 3, 2, and X divided by 5, 3, and X divided by 7, 2 ", is "print this number out" very similar to the syntax of daily life?
Next let's take a look at the for flow control statements on the outer layer. A for statement is a loop statement used to traverse a numerical value. The basic syntax format is as follows:
For (initialization; Termination judgment; Incremental ){
Execution statement
}
Before the loop is executed, the initialization part is executed first. For example, in our code, initialize