Recently in the Java teaching video, feel that the teacher is very good, while borrowing source code and notes to write this series of blogs, record their learning content, but also for the people to see learning.
Two days before coming home is the weekend, so Hey, you know!
Today continue to write the 5th article, from this beginning will involve some practical training questions, these in the study is very necessary oh ~
A while loop is written at the end of the previous article. So this is the beginning of the most commonly used for loops, for the loop in fact, the students have learned the C is more familiar with, it is actually equivalent to a flexible "while loop", Format: for (content 1; content 2; content 3), 1 is the initialization of the cyclic variable 2 is the judgment of the cyclic condition, 3 is the operation of the loop variable after each loop, the for loop in Java and C + + is exactly the same, here is no longer detailed.
Here are a few exercises:
<span style= "FONT-SIZE:14PX;" >/**
* While and for-loop practice topic: With the For loop output 1-1000 can be divisible by 5, and each row output 3/public
class Testwhilefor {
public static void Main (string[] args) {
int oddsum = 0; Used to hold odd and
int evensum = 0; Used to hold even and for
(int i=0;i<=100;i++) {
if (i%2!=0) {
oddsum = i;
} else{
Evensum + = i
}
}
System.out.println ("Odd and:" +oddsum);
System.out.println ("Even and:" +evensum);
for (int j = 1;j<=1000;j++) {
if (j%5==0) {
System.out.print (j+ "\ t"); (new skill get) note here, "/t" is tab, and after the Operation J is the character, each character will be separated by 8 characters distance
}
if (j% (5*3) ==0) {
System.out.println (); Three per line, then a multiple of 15 per encounter will be exchanged for one row
}}}
</span>
To print a small 99, the print format is as follows:
The implementation code is as follows:
<span style= "FONT-SIZE:14PX;" >//output 99 multiplication table public
class TestWhileFor02 {public
static void Main (string[] args) {for
(int m = 1; M <= 9 ; m++) {for
(int i = 1; I <= m; i++) {
System.out.print (i + "*" + M + "=" + (I * m) + "\ T");//End Tabulation
}
Sys TEM.OUT.PRINTLN ()///Change Line
}}
}
</span>
This is followed by the break and continue statements, which are also the most commonly used circular control statements:
There is a continue tag that is now less commonly used, just to get a good look at it, and now the program is not very supportive of using GOTO statements, which can cause confusion in execution:
<span style= "FONT-SIZE:14PX;" >/**
* Test break and Continue */public
class Testbreakcontinue {public
static void Main (string[] args) {
int total = 0;
System.out.println ("Begin");
while (true) {
total++;
int i = (int) math.round (M * math.random ());
if (i = =) {break
;
}
}
for (int i = i < i++) {
if (i% 3 = 0) {
continue
}
System.out.println (i);
}
Tagged continue, output 101-150 of all integers
int count = 0;
outer:for (int i = i < i++) {for
(int j = 2; J < I/2; J +) {
if (i% j = 0)
continue O uter;
}
System.out.print (i + " ");
}} </span>
Well, here's a very important role in object-oriented languages, which is a constituent member of the object (Class): Method. A method corresponds to a function within C + +, but it exists in a class and the following is a detailed description of the method:
The last one, about value passing and reference passing will be presented separately in the future, not in detail here.
The following is an example of the actual use of the method (the main method is a special method):
<span style= "FONT-SIZE:14PX;"
>/** * Test method/public class TestMethod {//Design Method Principle: The method is intended to be a function block, is a collection of statement blocks that implement a function.
When we design a method, it is best to keep the atomic nature of the method, that is, a method that completes only 1 functions, which is beneficial to our later expansion. public static void test01 (int a) {int oddsum = 0;///To hold odd and int evensum = 0;//For even-numbered and for (int i = 0; I <= A
i++) {if (i% 2!= 0) {oddsum = i;
else {evensum = i;
} System.out.println ("Odd and:" + oddsum);
System.out.println ("Even and:" + evensum); public static void test02 (int a,int b,int c) {for (int j = 1; J <= A; J +) {if (j% b = = 0) {SYSTEM.OUT.P
Rint (j + "T");
} if (j% (b * c) = = 0) {System.out.println ();
}} public static int add (int a,int b) {int sum = a+b; if (a==3) {return 0;
Return two: Ends the method's run (do not ignore), returns a value.
} System.out.println ("Output");
return sum;
public static void Main (string[] args) {test01 (1000); test02 (100,6,3);
1-100, can be divisible by 6, output 3 per line.
int s = Add (3,5);
System.out.println (s); }} </span>
Here is recursion, recursion is a very important basic knowledge in the computer programming algorithm, who White is an algorithm that oneself solves the problem by calling oneself.
<span style= "FONT-SIZE:14PX;" >/**
* Test recursive algorithm *
/public class Testrecursion {
static int a = 0;
public static void test01 () {
a++;
System.out.println ("test01:" +a);
if (a<=10) { //recursive header (prevent dead loops, necessary)
test01 ();
} else{ //Recursive body
System.out.println ("over");
}
public static void test02 () {
System.out.println ("testrecursion.test02 ()");
}
public static long factorial (int n) {
if (n==1) {return
1;
} else{return
n*factorial (n-1);
}
public static void Main (string[] args) {
test01 ();
System.out.println (factorial);
}
</span>
Look at the recursive schematic:
OK, this is the end of this article.
Calculate to now also wrote 5 Java learning notes, said really the beginning of the purpose is entirely for their own future review use, but in the process of writing I decided to introduce to others in the way, like the face of others to explain. At present, the contents of these five articles can be said to be very simple, foundation, even if you do not love programming computer professional students to a sophomore or junior will be very familiar with these, so these 5 I joined the subjective content is very little, but with the depth of learning, will be more and more close to the essence of Java, At that time I will probably put some of my feelings and understanding into the blog to communicate with you, I hope to see my blog's small partners if you find that the blog has introduced the wrong knowledge points please comment, greatly appreciated.