Java self study note Day05

Posted by dmccabe on Mon, 02 Dec 2019 04:36:20 +0100

Day05

java statement

for statement

/*
A: for Basic format of statement
		for ( Initialization expression; conditional expression; operation expression after loop){
			Circulatory body;
		}
B: Execution process
		* a Execute initialization statement;
		* b Execute the conditional judgment statement to see whether its return value is true or false;
			* If it is true, continue to execute;
			* false End the cycle;
		* c Execute loop body statement;
		* d Operation expression after loop execution;
		* e Go back to b for further implementation;
C:Case demonstration
		*Output "helloworld" 10 times in the console
*/
class Demo1_for
{
	public static void main(String[] args) 
	{
		int i;
		for (i = 0; i < 10 ; i++){
			System.out.println("HelloWorld");
		}
	}
}
/*
for statement considerations
	*a: judging whether the conditional statement is simple or complex is boolean;
	*b: if the loop body statement is one statement, the brace can be omitted, but if it is more than one statement, it cannot be omitted, but it is recommended not to omit;
	*c: Generally speaking, if there is a left brace, there is no semicolon; if there is a semicolon, there is no left brace;
*/

while statement

/*
*A: while Basic format of statement
		Initialization statement
	    while(Judgment condition statement){
			Loop body statement;
			Control condition statement
		}
*B: while Statement execution flow
		*a :Initialization statement;
		*b :Execute the conditional judgment statement to see whether its return value is true or false;
			*true Then continue to implement;
			*false End the cycle;
		*c :Execute loop body statement; '
		*d :Execute control condition statement;
		*e :Go back to b for further implementation;
*/
class Demo1_While
{
	public static void main(String[] args) 
	{
		int x = 1;
		while (x <=10)
		{	System.out.println("x = " + x);
			x++;
		}
	}
}

do... while statement

/*
*A: do...while Basic format of statement
		Initialization statement
	   do{
			Loop body statement;
			Control condition statement
		}while(Judgment condition statement);
*B: while Statement execution flow
		*a :Initialization statement;
		*b :Execute loop body statement; '
		*c :Execute control condition statement;
		*d :Execute the conditional judgment statement to see whether its return value is true or false;
			*true Then continue to implement;
			*false End the cycle;
		*e :Go back to b for further implementation;
*C: Case demonstration:
    Output 1-10;
*/
class Demo1_DoWhile
{
	public static void main(String[] args) 
	{	int x = 1;
		do
		{	System.out.println("x = " + x);
			x++;
		}
		while (x <= 10);
	}
}

for statement, do Differences between while statements:

*do...while statement executes the body of the loop at least once;
*Before the loop body of for and while statements is executed, it is necessary to determine whether the conditions are valid;
*After the for statement is executed, the variables will be released; after the while statement is executed, the variables will not be released and can continue to be used

Notes on loop statements in java language:

A: be sure to pay attention to the variables of the control condition statement, which is easy to cause a dead cycle after getting right;
B: two simple dead cycle modes:
	*while(true)(.....);
	*for( ; ;) {.......};

Practice

//Print four rows and five columns of stars
//Print single line print
class Demo1_ForFor 
{
	public static void main(String[] args) 
	{
		for (int i = 1 ; i <=4 ; i++ )				//Outer loop determines the number of rows
		{
			for (int j = 1 ; j <= 5 ; j++ )			//Inner loop determines the number of columns
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}
}
//multiplication table
/* Code optimization
	'\x' x Indicates any, and is an escape symbol. This is called an escape character;
	'\t'  tab Key position;
	'\r'  Enter
	'\n'  Line feed
	'\"'  Escape double quotes
	'\''  Escape single quotation mark
*/
class Test1_ForFor 
{
	public static void main(String[] args) 
	{
		for (int i = 1 ; i <= 9 ; i++)
		{
			for (int j = 1 ; j <= i ; j++ )
			{
				System.out.print(j + "*" + i + "=" +(i*j) + '\t');
			}
			System.out.println();
		}
	}
}

Control jump statement

/*
A: use scenario of break
	*Can only be used in switch and loop
*/
/*
A: continue Use scenario of / / terminate the loop, and then execute the expression after the loop
	*Can only be used in circulation;
*/

class Demo2_Continue
{
	public static void main(String[] args) 
	{
		for (int x = 1;x <= 10;x++ )
		{
			if (x ==4)
			{
				continue;
			}
			System.out.println("x = " + x);
		}
	}
}
//Control jump statement label
/*
For example: http://www.baidu.com
http: It is a label.
//www.baidu.com Is a single line note;
*/
class Demo3_Mark										//	mark marker
{
	public static void main(String[] args) 
	{
		a: for (int i =1;i <= 10; i++ )					//A is the label, as long as it is a legal identifier
		{
			System.out.println("x = " + i);
			b: for ( int j = 1; j <=10 ; j++ )
			{
				System.out.println("j = " + j);
				break a;
			}
		}
	}
}

Practice

//Don't change the range of i, output 13 times. i'm afraid i'm a fool
class Test 
{
	public static void main(String[] args) 
	{
		for (int i =1; i <=10 ;i++ )
		{
			if (i %3 ==0)
			{
				//break output twice;
				//continue; output 7 times
				System.out.println("I'm afraid it's a fool");
			}
			System.out.println("I'm afraid it's a fool");
		}
	}
}
class Demo4_Return			//Return to main method
{
	public static void main(String[] args) 
	{
		for (int i = 1; i <= 10 ;i++ )
		{
			if (i == 4)
			{
				return;
			}
		}
		System.out.println("Loop end");
	}
}
/*The difference between break, continue and return:
*return is the end method;
*break is to jump out of the loop;
*Continue is to terminate this cycle and not continue the next cycle*/

Overview and learning of methods

A:Why have a way?
	//Improve code reusability;
B:What is the method?
	//Code block to complete a specific function;
C:Method format
	*Modifier return value type method name(Parameter type parameter name1; Parameter type parameter name2....){
		//Method style sentence;
		return Return value;
	}
D: Format description of method
	*Modifier:Temporary use at the beginningpublic static ,Continue to learn later;
	*return type:Is the data type of the function result;
	*parameter:
		//Actual parameters:Is actually involved in the calculation;
		*Formal parameter:That's the definition of the method,For receiving actual parameters;
	*Parameter type:Is the data type of the parameter;
	*Method style sentence:Is the code to complete the function;
	*return :End method's;
	*Return value:It's the result of the function.YesreturnPass to caller;
E: Matters needing attention
	*A separate call to a method with a return value,Meaningless
	*Method not called not executed,
	*There is a parallel relationship between methods,Nested;
	*Method definition time,Comma separated parameters;
	*Method calls without passing parameters;
	*If the method has an explicit return,Be sure to use it.returnReturn.
    *If the return value isvoid,returnIt can be omitted.
	*The return value isvoidMethods of can only be called individually
	
//Give an example
/*
	Find the sum of two integers
	1.The sum of integers or integers;
	2.There are two unknowns involved in the operation

	How to write
	1.Clear return value type
	*/
	
import java.util.Scanner;
class Demo2_Sum 
{
	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter the first string");
		String x = sc.nextLine();
		System.out.println("Please enter a second string");
		String y = sc.nextLine();
		String sum = add(x, y);
		System.out.println("Two strings connected yes" + '\n' +sum);
	}
	public static String add(String a, String b){
		String sum = a + b;
		return sum;
		}
}

//Practice
	/*
Case demonstration:
	A:Enter two numbers on the keyboard to return the larger value of the two data;
	B:Input two data by keyboard, and compare whether the two data are equal;
*/
import java.util.Scanner;
class Test1_Method 
{
	public static void main(String[] args)					
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter the first integer");
		int x = sc.nextInt();								//The first number entered by keyboard is saved in x
		System.out.println("Please enter a second integer");
		int y = sc.nextInt();								//The second number entered by keyboard is saved in y
		int max = getMax(x, y);
		System.out.println("The larger of the two is" + max);
		boolean b1 = isEquals(x, y);
		System.out.println("Two numbers equal yes" + b1);
	}
	//Returns the larger of two integers
	public static int getMax(int a ,int b){
		return(a > b )? a : b;
	}
	//Compare two numbers for equality
	public static boolean isEquals(int c ,int d){
		return c == d;
	}
}
/*Matters needing attention
 If the return value is void,return can be omitted
 Methods whose return value is void can only be called separately
*/
/*
Case demonstration:
	Output star type in the console according to the number of rows and columns entered by keyboard
*/
import java.util.Scanner;
class  Test4_Method
{
	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter the number of lines");
		int row = sc.nextInt();
		System.out.println("Please enter the number of columns");
		int column = sc.nextInt();
		print(row, column);
		//System.out.println(print(row,column)); / / empty type is not allowed here
	}

	/*
	Output stars on the console
	1.No specific return value type, void;
	2.Explicit parameter list: number of rows and columns
	*/

	public static void print(int a ,int b){
		for (int i = 1; i <= a ; i++ )
		{
			for (int j = 1; j <= b ;j++ )
			{
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

//Call method to print multiplication table
import java.util.Scanner;
class Test3_method
{
	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter a 1~9 Number between");
		int num = sc.nextInt();
		print99(num);
	}
	
	//Print multiplication table
	public static void print99(int a){
		for (int i =1; i <= a ;i++ )			//Row number
		{
			for (int j = 1; j <= i; j++ )		//Column number
			{
				System.out.print(i + "*" + j + "=" + (i * j) + '\t');
			}
			System.out.println();
		}
	}
}

//Method overloading
//Overload: same method name, different parameter list, independent of return value type
/* Classification of overloads
	1.The number of parameters is different;
	2.Different parameter types;
		*Different order
*/
class Demo4_OverLoad 
{
	public static void main(String[] args) 
	{
		int sum1 = add(20,30);
		double sum2 = add(30,40,50);
		System.out.println(sum1);
		System.out.println(sum2);
	}

	/*Find the sum of two integers
	1.Return value type int
	2.Parameter list int a, int b;
	*/
	public static int add(int a ,int b){
		return a + b;
	}

	public static int add(int a ,int b, int c){
		return a + b +c;
	}
  
}

Topics: Java