Day 03 java Basics
Today's content introduction
circulation structure
circular nesting
control loop statement
Random number
Chapter 1 circulation structure
1.1 format and basic use of for loop
1.1.1 for loop statement format:
For (initialization statement; judgment condition statement; control condition statement){
Loop body statement;
}
1.1.2 execution process
A: Execute initialization statement
B: Execute the judgment condition statement to see whether the result is true or false
If false, the loop ends.
If true, continue.
C: Execute loop body statement
D: Execute control condition statement
E: Go back to B and continue
1.1.3 execution flow chart of for loop
1.1.4 code case I
package com.itheima;
/*
- Format of for loop statement:
-
for(Initialization statement;Judgment conditional statement;Control condition statement) {
-
Loop body statement;
-
}
- Execution process:
-
A:Execute initialization statement
-
B:Execute the judgment condition statement to see if the result is true still false
-
If it is false,Just end the cycle
-
If it is true,Just continue
-
C:Execute loop body statement
-
D:Execute control condition statement
-
E:go back to B continue
*/
public class ForDemo {
public static void main(String[] args) {
//Requirement: output HelloWorld 5 times on the console
//Original practice System.out.println("HelloWorld"); System.out.println("HelloWorld"); System.out.println("HelloWorld"); System.out.println("HelloWorld"); System.out.println("HelloWorld"); System.out.println("----------"); //Improve with for loop for(int x=1; x<=5; x++) { System.out.println("HelloWorld"); } }
}
1.1.5 practice of for loop
1.1.5.1 data acquisition of for loop exercise 1-5 and 5-1
1.1.5.2 code case II
package com.itheima;
/*
-
Requirements: obtain data 1-5 and 5-1
*/
public class ForTest {
public static void main(String[] args) {
//Original practice
System.out.println(1);
System.out.println(2);
System.out.println(3);
System.out.println(4);
System.out.println(5);
System.out.println("-------------");//for loop improvement for(int x=1; x<=5; x++) { System.out.println(x); } System.out.println("-------------"); //Obtaining data 5-1 for(int x=5; x>=1; x--) { System.out.println(x); } System.out.println("-------------");
}
}
1.1.5.3 calculation of for loop exercise: Data sum between 1-5
1.1.5.4 code case III
package com.itheima;
/* -
Requirement: find the sum of data between 1-5
-
analysis:
-
A:Define a summation variable with an initialization value of 0
-
B:Get 1-5 Data, using for Loop can be realized
-
C:Accumulate the data obtained each time to the summation variable
-
D:Output summation variable
*/
public class ForTest2 {
public static void main(String[] args) {
//Define a summation variable with an initialization value of 0
int sum = 0;
//Get 1-5 data, which can be realized by using the for loop for(int x=1; x<=5; x++) { //Accumulate the data obtained each time to the summation variable //sum = sum + x; sum += x; /* * First time: sum = sum + x = 0 + 1 = 1 * Second time: sum = sum + x = 1 + 2 = 3 * Third time: sum = sum + x = 3 + 3 = 6 * Fourth time: sum = sum + x = 6 + 4 = 10 * Fifth time: sum = sum + x = 10 + 5 = 15 */ } //Output summation variable System.out.println("sum:"+sum); }
}
1.1.5.5 for loop practice, find an even number between 1-100
1.1.5.6 code case IV
package com.itheima;
/*
- Demand: find the even sum between 1-100
- analysis:
-
A:Define the summation variable, and the initialization value is 0
-
B:Get 1-100 Even number between, with for Loop implementation
-
C:Get each acquired data and judge whether it is even
-
If it's an even number, add it up.
-
D:Output summation variable
*/
public class ForTest3 {
public static void main(String[] args) {
//Define the summation variable, and the initialization value is 0
int sum = 0;
//Get the even number between 1-100 and implement it with the for loop for(int x=1; x<=100; x++) { //Get each acquired data and judge whether it is even if(x%2 == 0) { //If it's an even number, add it up. sum += x; } } //Output summation variable System.out.println("sum:"+sum); }
}
1.1.5.7 printing daffodils for circular practice
1.1.5.8 code case V
package com.itheima;
/*
- Requirement: output all "daffodils" on the console
- Daffodils?
-
The so-called daffodil number refers to a three digit number, and the cubic sum of each digit is equal to the number itself.
-
Example: 153 is a daffodil number
-
1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153
- analysis:
-
A:Three digits actually tell us the range. This is used for Loop implementation
-
B:Get the bits, tens and hundreds of each three digit number
-
How to get a three digit number, a ten digit number and a hundred digit number?
-
Example: 153
-
Bits: 153%10
-
Ten: 153/10%10
-
Hundredth: 153/10/10%10
-
...
-
C:Compare the cube of one digit, ten digit and hundred digit with the number itself
-
If equal, it means that the number is the number of daffodils, which can be printed on the console
*/
public class ForTest4 {
public static void main(String[] args) {
//The three digits actually tell us the range, which is implemented with a for loop
for(int x=100; x<1000; x++) {
//Get the bits, tens and hundreds of each three digit number
int ge = x%10;
int shi = x/10%10;
int bai = x/10/10%10;
//Compare the cube of one digit, ten digit and hundred digit with the number itself if((ge*ge*ge+shi*shi*shi+bai*bai*bai) == x) { //If equal, it means that the number is the number of daffodils, which can be printed on the console System.out.println(x); } } }
}
1.1.5.9 statistics of the number of daffodils in the for cycle exercise
1.1.5.10 code case 6
package com.itheima;
/*
- Demand: how many "daffodils" are counted
- analysis:
-
A:Define statistical variables, and the initialization value is 0
-
B:Get three digits with for Loop implementation
-
C:Get data on each bit
-
D:Judge whether the data is the number of daffodils
-
If so, the statistical variables++
-
E:Output statistical variable
*/
public class ForTest5 {
public static void main(String[] args) {
//Define statistical variables, and the initialization value is 0
int count = 0;
//Get three digits and implement it with a for loop for(int x=100; x<1000; x++) { //Get data on each bit int ge = x%10; int shi = x/10%10; int bai = x/10/10%10; //Judge whether the data is the number of daffodils if((ge*ge*ge+shi*shi*shi+bai*bai*bai) == x){ //If so, the statistical variables++ count++; } } //Output statistical variable System.out.println("The number of daffodils is:"+count); }
}
1.2 overview and use of while loop format
1.2.1 while loop statement format
Basic format
While (judgment condition statement){
Loop body statement;
}
Extended format
Initialization statement;
While (judgment condition statement){
Loop body statement;
Control condition statement;
}
1.2.2 execution flow chart
1.2.3 code case 7
package com.itheima;
/*
- Statement format of while loop:
-
while(Judgment conditional statement) {
-
Loop body statement;
-
}
- Full format:
-
Initialization statement;
-
while(Judgment conditional statement) {
-
Loop body statement;
-
Control condition statement;
-
}
- Review the statement format of the for loop:
-
for(Initialization statement;Judgment conditional statement;Control condition statement) {
-
Loop body statement;
-
}
/
public class WhileDemo {
public static void main(String[] args) {
//Output HelloWorld 5 times on the console
//for loop implementation
/
for(int x=1; x<=5; x++) {
System.out.println("HelloWorld");
}
System.out.println("--------------");
*/
//while loop implementation int x=1; while(x<=5) { System.out.println("HelloWorld"); x++; } }
}
1.2.4 practice of while loop
1.2.4.1 data and data of 1-100 for while loop exercise
1.2.4.2 code case 8
package com.itheima;
/*
-
Requirement: find the data sum between 1-100.
/
public class WhileTest {
public static void main(String[] args) {
//for loop implementation
/
//Define summation variables
int sum = 0;for(int x=1; x<=100; x++) { //Accumulation is enough sum += x; } //Output results System.out.println("sum:"+sum); */ //while loop implementation //Define summation variables int sum = 0; int x=1; while(x<=100) { //Accumulation is enough sum += x; x++; } //Output results System.out.println("sum:"+sum);
}
}
1.3 overview and use of do while loop format
1.3.1 do while loop statement format
Basic format
do {
Loop body statement;
}While (judgment condition statement);
Extended format
Initialization statement;
do {
Loop body statement;
Control condition statement;
}While (judgment condition statement);
1.3.2 execution flow chart
1.3.3 code case 9
package com.itheima;
/*
-
Format of do... while loop statement:
-
do {
-
Loop body statement;
-
}while(Judgment conditional statement);
-
Full format:
-
Initialization statement;
-
do {
-
Loop body statement;
-
Control condition statement;
-
}while(Judgment conditional statement);
-
Execution process:
-
A:Execute initialization statement
-
B:Execute loop body statement
-
C:Execute control condition statement
-
D:Execute the judgment condition statement. See yes true still false
-
If it is false,Just end the cycle
-
If it is true,Just go back B continue
-
Exercise: summation case, daffodil case
/
public class DoWhileDemo {
public static void main(String[] args) {
//Output 5 HelloWorld cases on the console
/
for(int x=1; x<=5; x++) {
System.out.println("HelloWorld");
}
*/int x=1; do { System.out.println("HelloWorld"); x++; }while(x<=5);
}
}
package com.itheima;
/* -
The three loop statements can do the same thing, but there are other things:
-
do...while The loop statement executes the loop body at least once.
-
and for and while The loop statement should first judge the condition, and then see whether to execute the loop body statement.
-
Minor differences between for loop and while loop:
-
for After the loop ends, the initialized variables cannot be used.
-
and while After the loop ends, the initialized variables can continue to be used.
-
Recommended sequence:
-
for -- while -- do...while
/
public class DoWhileDemo2 {
public static void main(String[] args) {
/
int x = 3;
do {
System.out.println("love life, love Java");
x++;
}while(x < 3);
*/
/* int x = 3; while(x < 3) { System.out.println("Love life, love Java "); x++; } */ /* for(int x=1; x<=5; x++) { System.out.println("Love life, love Java "); } System.out.println(x); */ int x=1; while(x <= 5) { System.out.println("Love life, love Java"); x++; } System.out.println(x); }
}
1.4 differences between the three cycles
1.4.1 overview of differences
Although it can complete the same function, there are still small differences:
The do... while loop executes the loop body at least once.
for loop and while loop will execute the loop body only when the condition is true
Minor differences between for loop statement and while loop statement:
Use difference: the variable controlled by the control condition statement can no longer be accessed after the end of the for loop, and the while loop can continue to be used. If you want to continue to use it, use while, otherwise it is recommended to use for. The reason is that when the for loop ends, the variable disappears from memory, which can improve the efficiency of memory use.
1.4.2 code case x
package com.itheima;
/*
- The three loop statements can do the same thing, but there are other things:
-
do...while The loop statement executes the loop body at least once.
-
and for and while The loop statement should first judge the condition, and then see whether to execute the loop body statement.
- Minor differences between for loop and while loop:
-
for After the loop ends, the initialized variables cannot be used.
-
and while After the loop ends, the initialized variables can continue to be used.
- Recommended sequence:
-
for -- while -- do...while
/
public class DoWhileDemo2 {
public static void main(String[] args) {
/
int x = 3;
do {
System.out.println("love life, love Java");
x++;
}while(x < 3);
*/
/* int x = 3; while(x < 3) { System.out.println("Love life, love Java "); x++; } */ /* for(int x=1; x<=5; x++) { System.out.println("Love life, love Java "); } System.out.println(x); */ int x=1; while(x <= 5) { System.out.println("Love life, love Java"); x++; } System.out.println(x); }
}
Chapter 2 loop nesting
2.1.1 please output a star () pattern with 4 rows and 5 columns
2.1.1.1 case code Xi
package com.itheima;
/
- Requirement: Please output a star (*) pattern with 4 rows and 5 columns.
- result:
- Circular nesting: that is, the circular body sentence itself is a circular sentence.
- Conclusion:
-
The outer loop controls rows and the inner loop controls columns
*/
public class ForForDemo {
public static void main(String[] args) {
//Original practice
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("-------------------");
//Although we have fulfilled the requirements of the topic, it is not good //If we have many such requirements, and the rows and columns are changed //So we need to improve the current code writing //I want to output one on a line* //System.out.println("*"); //I want to output five on a line* /* System.out.println("*"); System.out.println("*"); System.out.println("*"); System.out.println("*"); System.out.println("*"); */ //The reason for this result is that system out. Println () adds a newline after each output of the content //Is there a solution? have //Use system out. Print () can be solved /* System.out.print("*"); System.out.print("*"); System.out.print("*"); System.out.print("*"); System.out.print("*"); */ /* //Improve code with loop for(int x=1; x<=5; x++) { System.out.print("*"); } //Use the following content to realize line feed System.out.println(); //Five in the second row* for(int x=1; x<=5; x++) { System.out.print("*"); } System.out.println(); //Five in the third row* for(int x=1; x<=5; x++) { System.out.print("*"); } System.out.println(); //Five in the fourth row* for(int x=1; x<=5; x++) { System.out.print("*"); } System.out.println(); */ //Repeated code is executed many times and improved with loop for(int y=1; y<=4; y++) { for(int x=1; x<=5; x++) { System.out.print("*"); } System.out.println(); } System.out.println("-------------------"); for(int y=1; y<=7; y++) { for(int x=1; x<=8; x++) { System.out.print("*"); } System.out.println(); } }
}
2.1.2 printing regular triangles in circular nesting exercise
2.1.2.1 case code 12
package com.itheima;
/*
- Requirements: Please output the following graphics
-
*
/
public class ForForTest {
public static void main(String[] args) {
//Through simple observation, we found that this is a shape with 5 rows and varying number of columns
//First, let's implement a shape of 5 rows and 5 columns
for(int x=1; x<=5; x++) {
for(int y=1; y<=5; y++) {
System.out.print("");
}
System.out.println();
}
System.out.println("----------------------");
//Although we implemented a shape of 5 rows and 5 columns //But that's not what we want //The shape we want changes with the number of columns //What shall I do? //First line: 1 y=1; y<=1 //Line 2: 2 y=1; y<=2 //Line 3: 3 y=1; y<=3 //Line 4: 4 y=1; y<=4 //Line 5: 5 y=1; y<=5 //We need a variable that starts with 1 and ends with 5 int z = 1; for(int x=1; x<=5; x++) { for(int y=1; y<=z; y++) { System.out.print("*"); } System.out.println(); z++; } System.out.println("----------------------"); //We have realized our requirements now, but we will find that if we continue to observe //First time: the value of x is 1 //Second time: the value of x is 2 //... //x and z are the same change process. In this case, we can omit z and replace it directly with x for(int x=1; x<=5; x++) { for(int y=1; y<=x; y++) { System.out.print("*"); } System.out.println(); } System.out.println("----------------------"); }
}
2.1.3 printing 99 multiplication table for circular nesting exercise
2.1.3.1 case code XIII
package com.itheima;
/*
-
Requirement: print 99 multiplication table on the console
-
\t: Transfer character, indicating the position of a tab key
/
public class ForForTest2 {
public static void main(String[] args) {
//Print a star with 9 rows and 9 columns first (the columns are variable)
for(int x=1; x<=9; x++) {
for(int y=1; y<=x; y++) {
System.out.print("");
}
System.out.println();
}
System.out.println("------------");/* 1*1=1 1*2=2 2*2=4 1*3=3 2*3=6 3*3=9 ... */ for(int x=1; x<=9; x++) { for(int y=1; y<=x; y++) { System.out.print(y+"*"+x+"="+y*x+"\t"); } System.out.println(); }
}
}
Chapter 3 control loop statement
3.1 overview and use of jump control statement break
3.1.1 usage scenario of break
In the select structure switch statement
In a loop statement
It is meaningless to leave the existence of the usage scenario
Function of break:
Jump out of single layer cycle
Jump out of multi-layer loop
– tabbed jump out
– format:
Tag name: Circular statement
Tag names should conform to Java Naming Conventions
3.1.2 code case 14
package com.itheima;
/*
- Break: break
- Usage scenario:
-
A:switch Statement, used to end switch sentence
-
B:In a loop statement, used to end a loop
- How to use:
-
A:Jump out of single layer cycle
-
B:Jump out of multi-layer loop
-
Use tagged statement format.
*/
public class BreakDemo {
public static void main(String[] args) {
//break can be used to end the current loop.
for(int x=1; x<=5; x++) {
if(x == 3) {
break;
}
System.out.println("HelloWorld");
}
System.out.println("-----------------------");
//If it is a multi-layer loop, which loop does break end? //break ends the cycle closest to him //If I want to jump out of the outer cycle, can I? //sure. How to achieve it? //Tagged statement: //Format: tag name: statement wc:for(int x=1; x<=3; x++) { nc:for(int y=1; y<=4; y++) { if(y == 2) { break wc; } System.out.print("*"); } System.out.println(); } }
}
3.2 overview and use of jump control statement continue
3.2.1 usage scenario of continue
In a loop statement
It is meaningless to leave the existence of the usage scenario
Function of continue:
Single layer cycle contrast break, and then summarize the differences between the two
break exits the current loop
continue exit this cycle
3.2.2 code case XV
package com.itheima;
/*
- Continue: continue means to continue
- Usage scenario:
-
In circulation. There is no point in leaving the usage scenario.
- The difference between break and continue:
-
break:Jump out of the whole cycle
-
continue:Jump out of this operation and enter the next execution
*/
public class ContinueDemo {
public static void main(String[] args) {
for(int x=1; x<=5; x++) {
if(x == 3) {
continue;
}
System.out.println("HelloWorld"+x);
}
}
}
Chapter 4 Random number
4.1 overview and use of random
4.1.1 use steps of random
• function:
Used to generate a random number
• use steps (similar to Scanner)
Guide Package
import java.util.Random;
create object
Random r = new Random();
Get random number
int number = r.nextInt(10);
The generated data is between 0 and 10, including 0 and excluding 10.
The 10 in the bracket can be changed. If it is 100, it is the data between 0 and 100
4.1.2 case code 16
package com.itheima;
import java.util.Random;
/*
- Random: a class used to generate random numbers. The usage is similar to Scanner.
- Use steps:
-
A:Guide Package
-
import java.util.Random;
-
B:create object
-
Random r = new Random();
-
C:Get random number
-
int number = r.nextInt(10);
-
Scope of acquisition:[0,10) Including 0, excluding 10
*/
public class RandomDemo {
public static void main(String[] args) {
//Create object
Random r = new Random();
for(int x=1; x<=10; x++) { //Get random number int number = r.nextInt(10); System.out.println("number:"+number); } System.out.println("--------------"); //How to get a random number between 1-100? int i = r.nextInt(100)+1; System.out.println(i); }
}
4.2 figure guessing game case:
4.2.1 case code 17
package com.itheima;
import java.util.Random;
import java.util.Scanner;
/*
- Demand: guessing numbers games.
-
The system generates a 1-100 Between random numbers, please guess how much this data is?
- analysis:
-
A:The system generates a 1-100 Random number between
-
Random r = new Random();
-
int number = r.nextInt(100)+1;
-
B:Enter the data we want to guess with the keyboard
-
C:Compare the two data to see if our guess is correct
-
If it is big, hint: you guess the data is big
-
If it's small, hint: you guess the data is small
-
If equal, hint: Congratulations, you guessed right
-
D:In order to guess the data many times, we have to join the loop, and we don't know how many times we can guess. What shall I do??
-
Dead cycle: while(true) {...}
-
for(;;) {...}
*/
public class RandomTest {
public static void main(String[] args) {
//The system generates a random number between 1 and 100
Random r = new Random();
//Get random number
int number = r.nextInt(100)+1;
//Multiple guess data while(true) { //Create keyboard entry object Scanner sc = new Scanner(System.in); //Give tips System.out.println("Please enter the integer you want to guess(1-100): "); int guessNumber = sc.nextInt(); //Compare the two data to see if our guess is correct if(guessNumber > number) { System.out.println("You guessed the data"+guessNumber+"Big"); }else if(guessNumber < number) { System.out.println("You guessed the data"+guessNumber+"Small"); }else { System.out.println("Congratulations, you guessed right"); break; //Jump out of loop } } }
}