Method: A fragment that completes a specific function code.
Format: [modifier 1 modifier 2 ...] Return value type method name (formal parameter list) {
Java statement;
}
Formal parameters: Used to receive data from outside input when the method is invoked.
Argument: The data that is actually passed to the method when the method is called.
Return value: Returns to the environment data that called it after the method has finished executing
Return value type: Pre-agreed return value data type, if no return value, you must give void
The calling method is used in the Java language: Object name. Method Name (argument list).
Number of arguments, data type and order must match the invocation of the method declaration form parameter list
The return statement terminates the run of the method and specifies the type to return
Only values are passed in Java
Simple reading of the keyboard input string:
Scanner input =new Scanner (systrm.in);
String str =input.nextline ();
String str =input.next ();
int Str=input.nextint ();
System.out.println (str);
Sequential structure:
Java's basic structure is the sequential structure, unless otherwise specified, in order to execute a sequential structure of a simple algorithm, between statements and statements, between boxes and boxes in order from top to bottom, it is a number of sequentially executed processing steps.
If single structure:
Double I=6*math.random ();//math.random () produces a random number of 0~1
Double J=6*math.random ();//6*math.random () generation [0,5]
Double K=6*math.random ();
IoT count= (int) (I+J+J);
if (count>15) {
System.out.printlan ("Good luck");
}
if (count>=10&&count<=15) {
System.out.printlan ("Luck Class");
}
System.out.println ("Got" +count+ "points");
If else double-select structure: If the condition is true to perform an action, false to perform another operation
If the conditional expression is true, the statement block 1 is executed, otherwise the statement block 2 is executed.
Double R =4*math.random ();//Generate random number [0,4]
Double Are=math.pi*math.pow (r,2); the PI value defined in//math Math.pow (R.2) is the square of R
Double Cir =2*math.pi*r;
System.out.println ("Radius:" +r);
System.out.println ("area:" +are);
System.out.println ("Perimeter is:" +cir);
if (ARE>=CIR) {
System.out.println ("area greater than perimeter");
}else{
SYSTEM.OUT.PRINTLN ("circumference greater than area");
}
If else if else multi-select structure
if (Boolean expression 1) {
Statement Block 1;
}else if (Boolean expression 2) {
Statement Block 2;
}else if (boolean expression N) {
statement block N;
}else{
Statement block N+1;
}
public class IfTest3 {
public static void Main (string[] args) {
int age = (int) (Math.random ());
System.out.print ("Age is" + Ages + ", belongs to");
if (age < 15) {
System.out.println ("Children,");
} else if (age < 25) {
SYSTEM.OUT.PRINTLN ("Youth,");
} else if (age < 45) {
System.out.println ("Middle-aged,");
} else if (age < 65) {
System.out.println ("Middle-aged,");
} else if (age < 85) {
System.out.println ("old Age,");
} else {
System.out.println ("Loushing,");
}
}
}
Switch multi-select structure: Perform different actions depending on the expression
Switch statement: The case label must be an integer or an enumeration and cannot be a string.
switch (expression) {//byte,int,char,short and enum-type.
Case value 1;//value 1 must be constant
Statement sequence;
break;//Each statement ends with a break
Case value 2;
Statement sequence;
Break
...
default;//dispensable
Default statement;
}
The switch statement executes at the case label that matches the value of the expression and executes until the break statement or at the end of the switch statement. Does not match the case value, the next statement is entered.
public class vir{
public static void Main (String[]args) {
char c = ' a ';
int rand= (int) (26*math.random ());//(int) cast to int type produces a random number of [0,25]
Char c2= (char) (C+rand);
System.out.print (c2+ ";");
Switch (C2) {
Case= ' a '://Use case through, output all characters until break is encountered
Case= ' E ':
Case= ' I ':
Case= ' O ':
case= ' u ':
System.out.println ("vowel");
Break
Case= ' y ':
Case= ' W ':
System.out.println ("Semivowels");
Break
Default
System.out.println ("consonant");
}
}
Example:
System.out.print ("Please enter the month:");
int month = (int) (1+math.random () *12);
if (month>0&&month<13) {
Switch (month) {
Case 2:
System.out.println ("28 Days");
Break
Case 4:
Case 6:
Case 9:
Case 11:
System.out.println ("30 days");
Break
Default
System.out.println ("31 days");
}
}else{
System.out.println ("The month you entered is not legal! ");
}
While loop structure;
While statement format:
while (Boolean expression) {
Circulation body;
}
1. When the loop starts, the value of the Boolean expression is evaluated once, and if the condition is true, the loop body is executed. And for each subsequent cycle. will be recalculated once.
2. The statement should loop toward the end of the statement, otherwise die loop.
public class test{
public static void Main (string[] args) {
int i=0;
int sum=0;
while (i<100) {//Loop condition
sum+=i;//Calculate sum
i++;
}
System.out.println ("sum=" +sum);
}
}
Do and loop
bo=
Circulation body;
}while (boolean expression);
Execute the judgment first, at least once.
public class test{
public static void Main (string[] args) {
int i=0;
int sum=0;
do{
Sum+=i;
i++;
}while (i<=100);
System.out.println ("sum=" +sum);
}
}
The difference between while and dowhile
Dowhile is executed at least once.
int a = 0;
while (a<0) {
System.out.println (a);
i++;
}
SYSTEM.OUT.PRINTLN ("******");
a=0;
do{
System.out.println (a);
i++;
}while (a<0);
For loop
1.for Loop statement supports a common structure for iterations
Format:
for (initial expression; boolean-expression; stepping) {
Circulation body;
}
The For loop is initialized before the first iteration, then the conditional test is performed, and it is stepped at each iteration;
1. Initialize the initial value of the loop variable
2. Conditional expressions are judged as Boolean types
3. The iterative factor controls the increment or decrement of the cyclic variable
For (an initial expression; a Boolean expression; an iteration factor) {
Circulation body;
}
public class Fortest {
public static void Main (String args[]) {
int sum = 0;
for (int i = 0; I <=; i++) {
sum + = i;
}
System.out.println ("sum=" + Sum);
for (int i=9;i>0;i--) {
System.out.println (i);
}
for (int i=90;i>0;i-=3) {
System.out.println (i);
}
}
}
Java loop variable