# Circular statements - case exercises

catalogue

Case: printing data

Case: summation

Case: even sum

Case: number of daffodils

Case: count the number of daffodils

Case: Mount Everest

Case: guessing numbers

# Case: printing data

Demand: output data from 1 to 5 and 5 to 1 on the console

analysis:
1. Use the for loop statement to output data 1-5 and 5-1
for (initialization statement; conditional judgment statement, execute loop body statement)
2. From 1 to 5, the setting initialization starts from 1,
Set the judgment condition to continue the execution when it is not up to 5, and end the execution when it is up to 5,
Increase 1 every time from 1 to 5, and set the condition control to + 1 every time
for(int a = 1; a <= 5; a++)
3. Write the repeatedly executed things into the loop structure
for(int a = 1; a <= 5; a++) {
System.out.println(a);
}
From 5 to 1, setting initialization starts from 5,
Set the judgment condition to continue the execution when it does not reach 1, and end the execution when it reaches 1,
Reduce 1 every time from 5 to 1, and set the condition control to - 1 every time
for(int a = 5; a >= 1; a--)
3. Write the repeatedly executed things into the loop structure
for(int a = 5; a >= 5; a--) {
System.out.println(a);

Code demonstration:

```public class ForNumber {
public static void main(String[] args) {
//Use the for loop statement to output data 1-5
for (int a = 1; a <= 5; a++) {
System.out.println(a);
}

System.out.println("----");

for (int a = 5; a >= 1; a--) {
System.out.println(a);
}
}
}```

# Case: summation

Requirements: sum the data between 1 and 5, and output the sum result on the console

analysis:
1. The summation result must be saved with a variable to save the summation result. The initial value is 0
int sum = 0
2. Use the for loop statement to complete the data setting initialization statement between 1 and 5, and set the judgment condition to continue to execute when it is less than 5,
The execution ends when it reaches 5. Increase 1 every time from 1 to 5, and set the condition control to + 1 every time
fou(int a = 1; a <= 5; a++)
3. Write the repeated things into the loop structure. The repeated thing here is to add the data i to the variable sum used to save the final summation
fou(int a = 1; a <= 5; a++) {
sum += i
4. When the cycle is completed, the final data will be printed out
System.out.println(sum);
}

Code demonstration:

```public class ForNumber1 {
public static void main(String[] args) {
int sum = 0;
/*Use the for loop statement to complete the data setting initialization statement between 1 and 5, and set the judgment condition to continue to execute when it is less than 5,
The execution ends when it reaches 5. Increase 1 every time from 1 to 5, and set the condition control to + 1 every time
*/
for (int a = 1; a <= 5; a++) {
sum += a;
}

/*
First sum: sum + a == 0 + 1 == 1;
Second sum: sum + a == 1 + 2 == 3;
The third sum: sum + a == 3 + 3 == 6;
The fourth sum: sum + a == 6 + 4 == 10;
The fifth time: sum + a == 10 + 5 == 15;

First time i == 1; sum == 0 adds up to 1, sum = = 1
The second time i needs to do + + operation = = 2; sum == 1, sum equals 3, sum = = 3
The third time i==2, and then do + + operation i==3; sum==3, sum equals 6, sum = = 6
For the fourth time, i==3, and then do the + + operation, i==4, sum==6. The sum is equal to 10, and sum = = 10
The fifth time i==4, and then do the + + operation i==5, sum==10. The sum is equal to 15, and sum is equal to 15
The sixth time i==5, and then do the + + operation i==6, the conditional judgment statement will be judged as false, and the operation will stop
*/

//When the cycle is completed, the final data will be printed out
System.out.println(sum);//So sum==15

//Self extension, demand 1 to 999 and

/*The summation result must be saved with a variable, which is used to save the summation result. The initial value is 0
int number = 0
*/
int number = 0;

//Use the for loop statement to complete the data setting initialization statement between 1 and 5, and set the judgment condition to continue to execute when it is less than 5,
//    The execution ends when it reaches 5. Increase 1 every time from 1 to 5, and set the condition control to + 1 every time
for (int b = 1; b <= 999; b++) {
number += b;
}

//When the cycle is completed, the final data will be printed out
System.out.println(number);
}
}```

# Case: even sum

Requirements: find the even sum between 1-100, and output the sum result on the console.

analysis:
1. The summation result must be saved with a variable to save the result. The initial value is 0
int number = 0
2. Use the for statement, use the for loop statement to complete the data setting initialization statement between 1-100, and set the judgment condition to continue execution when it is less than 100,
The execution ends when it reaches 100, increases by 1 every time from 1 to 100, and sets the condition control by + 1 every time
Set initialization statement: int a = 1
Set the judgment condition as: a < = 100
Set the condition control to + 1: a each time++
for(int a = 1; a <= 100; a++) {

}
3. Even numbers between 1 and 100 are required, and if statements are embedded to help exclude odd numbers between 1 and 100
for(int a = 1; a <= 100; a++) {
if(a % 2 == 0) {
}
}
4. When the cycle is completed, print out the final result
System.out.println(a)

Code demonstration:

```public class ForNumber2 {
public static void main(String[] args) {
//The summation result must be saved with a variable, which is used to save the result. The initial value is 0
int number = 0;

//Set initialization statement
int a = 1;

//Use the for loop statement to embed the if statement constraints and print the final result of the loop statement
for (a = 1; a<=100; a++) {

//Judge whether a is an even number. If it is an even number, let them add up
if (a % 2 == 0) {
number += a;
}
}

//If the output statement is placed outside the loop statement, the output result will not be, so the loop result will only get the final result
System.out.println(number);
}
}```

# Case: number of daffodils

Requirement: output all "daffodils" on the console

What is "daffodil number"?

• The number of daffodils is three

For example: 111, 222, 333, 370, 371, 520, 999

• Daffodils are the sum of the number cubes of one digit, ten digit and hundred digit, which is equal to the original number

For example:

123 is 1 ³+ two ³+ three ³= 1 + 8 + 27 = 123, it's not the number of daffodils

371 is 3 ³+ seven ³+ one ³= 27 + 343 + 1 = 371 is the number of daffodils

analysis:
1. How to find a three digit number
371 % 10 = 1； 1 is the result of dividing the original number by 10

2. How to find the three digit hundred digit number
371 / 100 = 3 is the result of dividing the original number by one hundred (integer division)

3. How to find the three digit ten digit number
371 / 10 % 10 = 7; When the original number is divided by 37, 7 becomes one digit, and then ten digits are obtained according to analysis 1

Code demonstration:

```public class ForDemo2 {
public static void main(String[] args) {
//Set initialization statement
int i = 100;

/*Use the for loop statement to complete the data judgment between 100-999 and end at 999
And set condition judgment and condition control
It is also possible to write for (I = 100, I < 1000; I + +)
Since the number of daffodils is three digits, the starting value is 100,
Terminate the circular statement after setting the condition judgment I > 999
Condition control: it needs to traverse three digits, so i + + is used to cycle judgment in turn
*/
for(i =100; i <= 999; i++) {
//Three hundred digits
int ge = i % 10;
int bai = i /100;
int shi = i / 10 % 10;
//Judge whether the cube of the three numbers of conditional calculation is equal to i
if(ge * ge * ge + shi * shi * shi + bai * bai* bai == i) {
//If the sum of the cubes of the above ten hundred digits is equal to i, this i is our daffodil number and outputs the result
System.out.println(i);
}
}
}
}```

# Case: count the number of daffodils

Demand: count the number of daffodils, and output them on the console

analysis:

① Define a variable to count the number of daffodils, with an initial value of 0

int b = 0;

② Use the for loop to traverse 100-999 to get the number of daffodils

③ Requirements for setting the number of daffodils

④ Use the if statement to judge. If it is found that the number of daffodils meets the requirements, give the variable b+1

⑤ Output the number of daffodils and daffodils on the console

```public class ShuiXianHua {
public static void main(String[] args) {
//Define a variable to receive the number of daffodils
int b = 0;
//Prompt Hu
System.out.println("The number of daffodils is:");
//Traverse 100-999
for (int a = 100; a < 1000; a++) {
//Requirements for setting the number of daffodils
int bai = a / 100;
int shi = a / 10 % 10;
int ge = a % 10;
//The number of daffodils that meet the requirements is the number of daffodils
if (bai * bai * bai + shi * shi * shi + ge * ge * ge == a) {
//Output daffodils
System.out.println(a);
//If one number meets the requirements, add 1 to the value of b (you can get the number of daffodils)
b++;
}
}
//Number of output daffodils
System.out.println("How many daffodils are there" + b++ +"individual");
}
}```

# Case: Mount Everest

Demand: the highest mountain in the world is Mount Everest (8848.43 M = 8848430 mm). If I have a piece of paper large enough, its thickness is 0.1 mm. How many times can I fold it to the height of Mount Everest?

analysis:
1. Define a variable to count the folding times of paper, and the initial value is 0
int a = 0
2. Using while loop statements
Define initialization statement to define the thickness of paper: double d = 0.1
Set the condition to judge that the thickness of the sentence does not exceed the height of Mount Everest. If it exceeds, it will stop: d < = 8848430;
Set the conditional control statement to double the thickness in the loop: d *= 2;
Set a counter to perform the accumulation operation in the cycle, corresponding to how many times it is superimposed
a++；
3. Print the value of the counter

Code demonstration:

```public class Mt_EverestDemo {
public static void main(String[] args) {
//Define a variable to count the folding times of paper. The initial value is 0
int a = 0;

/*2. Using while loop statements
Define initialization statement: double d = 0.1
Set condition judgment statement: d < = 8848430
Set condition control statement: d *= 2;
How many times is the accumulation operation performed in the loop
a++
*/
double d = 0.1;
while (d <= 8848430) {
d *= 2;
a++;
}

//Print the value of the count variable
System.out.println(a);
}
}```

# Case: guessing numbers

Case: guessing numbers

Requirements: the program automatically generates a random number between 1-100. Use the program to guess what the number is?

When you guess wrong, give corresponding tips in different situations

For example:

• If the guessed number is larger than the real number, you will be prompted that the guessed data is larger
• If the guessed number is smaller than the real number, it indicates that the guessed data is large
• If the number you guessed is equal to the real number, you will be prompted to congratulate you on your correct guess

analysis:
1. You need to generate a random number guide packet
import java.util.Random;
2. Objects need to be created
Random r = new Random();
3. It is necessary to generate a random number and save it with variables
Int x = r.nextint (numeric range);
4. Guess random numbers by keyboard entry
create object
Scanner sc = new Scanner(System.in);
5. Use a variable to receive the data entered by the keyboard
int y = sc.nextInt();
6. Because you can't predict how many times you can guess correctly, you need to use while to cycle repeatedly,
while is usually used to describe loops of unknown number.
While {}
7. Branch statements are required to compare the input numbers with the data generated by the system. Use here
if... else... if format. Guess according to different situations. The result shows that when you guess correctly, use break to end the loop
If (condition: larger than the guessed number){
Output statement body
}Else if (the condition is smaller than the guessed number){
Output statement body
} else {
Output statement body
break;
}

Code demonstration:

```//1 Guide Package

import java.util.Random;
//2 Guide Package
import java.util.Scanner;

public class GuessNumber {
public static void main(String[] args) {
/*1 Create objects. Note that the creation objects and random numbers of 1 cannot be placed in the dead loop,
Otherwise, we will get a new random number every cycle, which may cause us to never guess correctly
*/
Random r = new Random();
//1 generate random number and set range
int x = r.nextInt(100) + 1;

//Here, you need to use an endless loop to guess while for many times. You need to enclose the following if statement
while (true) {
System.out.println("Please enter the number you want to guess:");

/*2 Use the keyboard input to receive data. The keyboard input needs to be placed in the while loop, followed by the loop. If you don't guess correctly, you need to continue to input
Keyboard entry must be placed under the created object
*/
Scanner sc = new Scanner(System.in);
int y = sc.nextInt();

/*Comparing the input numbers with the system generated data requires the use of branch statements. Use here
if...else...if Format, guess according to different situations, and the result is displayed
*/
if (y > x) {
System.out.println("You guessed the number" + y + "Big");
} else if (y < x) {
System.out.println("You guessed the number" + y + "Small");
} else {
System.out.println("Congratulations, you guessed right");

//After you have guessed correctly, use break to end. If you don't fill in break, you will be right and let you continue to guess
break;
}
}
}
}
```

Tags: Java

Posted by richblend on Thu, 14 Apr 2022 23:58:42 +0930