Day05
java statement
for statement
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
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
class Demo1_ForFor
{
public static void main(String[] args)
{
for (int i = 1 ; i <=4 ; i++ )
{
for (int j = 1 ; j <= 5 ; j++ )
{
System.out.print("*");
}
System.out.println();
}
}
}
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
*/
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);
}
}
}
class Demo3_Mark
{
public static void main(String[] args)
{
a: for (int i =1;i <= 10; i++ )
{
System.out.println("x = " + i);
b: for ( int j = 1; j <=10 ; j++ )
{
System.out.println("j = " + j);
break a;
}
}
}
}
Practice
class Test
{
public static void main(String[] args)
{
for (int i =1; i <=10 ;i++ )
{
if (i %3 ==0)
{
System.out.println("I'm afraid it's a fool");
}
System.out.println("I'm afraid it's a fool");
}
}
}
class Demo4_Return
{
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
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
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();
System.out.println("Please enter a second integer");
int y = sc.nextInt();
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);
}
public static int getMax(int a ,int b){
return(a > b )? a : b;
}
public static boolean isEquals(int c ,int d){
return c == d;
}
}
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);
}
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();
}
}
}
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);
}
public static void print99(int a){
for (int i =1; i <= a ;i++ )
{
for (int j = 1; j <= i; j++ )
{
System.out.print(i + "*" + j + "=" + (i * j) + '\t');
}
System.out.println();
}
}
}
//Method overloading
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);
}
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;
}
}