Learning Java loop nested loop freshwater lake ranking

Level 1: learning Java loop nested loop freshwater lake ranking

Task description

Task: there are four major freshwater lakes in China, namely Dongting Lake, Hongze Lake, Poyang Lake and Taihu Lake. A said: Dongting Lake is the largest, Hongze Lake is the smallest, and Poyang Lake is the third; B said: Hongze Lake is the largest, Dongting Lake is the smallest, Poyang Lake is the second and Taihu Lake is the third; C said: Hongze Lake is the smallest and Dongting Lake is the third; D said: Poyang Lake is the largest, Taihu Lake is the smallest, Hongze Lake is the second and Dongting Lake is the third. Please find out the correct ranking of the Four Great Lakes according to what everyone said.

Note: there is no equal situation among the four lakes, and each person is only right in one sentence.

Relevant knowledge

Nested loop

Nested loop means that a loop contains one or n other loops. while, do... while and for loops can be nested with each other. For example, there can be a for loop in a for loop and a do... while loop in a for loop.

Nested circular use case: print the figure shown below.

 
  1. *
  2. **
  3. ***

The code is as follows:

 
  1. public static void main(String[] args) {
  2. int i, j;
  3. For (I = 1; I < = 3; I + +) {/ / number of outer loop control lines
  4. For (J = 1; J < = I; j + +) {/ / inner loop printing*
  5. System.out.print("*"); // Note that it is not println
  6. }
  7. System.out.print("\n"); // Line feed
  8. }
  9. }

From the above cases, it can be concluded that the outer loop of the nested loop iterates once, that is, wrap once, and the inner loop executes once, that is, loop prints all * of a line.

Using Java to solve logic problems

Four students in a class went out. One of them did good deeds without leaving a name. After the letter of praise came, the head teacher asked who did good deeds. A said: not me; B said: Yes, C; C says: it's D; D said: C nonsense. It is known that three people are telling the truth and one is lying. Now, based on this information, find out who has done good.

Step 1: create variables to represent a, B, C and D respectively; Step 2: translate what everyone says into logical statements. For example, what a says can be expressed as! = A ', what B says can be expressed as = ='c', what C says can be expressed as = ='d ', and what D says can be expressed as! =' C'; Step 3: use the ternary operator to convert the truth and falsehood of the speech into specific values. For example, 1 means that the speech is true and 0 means that the speech is false; Step 4: convert what four people say into a ternary operator (if it is true, it is assigned as 1, if it is false, it is assigned as 0), and then sum the values of all ternary operators. If it is equal to 3, it means that three people are telling the truth and one is lying.

The specific codes are as follows:

 
  1. public static void main(String[] args) {
  2. char t;
  3. int sum;
  4. //Start the cycle from A. when sum is equal to 3, the person who has done good will be output
  5. for (t='A'; t<='D'; t++) {
  6. sum = (t!='A'?1:0) + (t=='C'?1:0)+(t=='D'?1:0)+(t!='D'?1:0);
  7. if (sum==3)
  8. System.out.printf("the person who does good is" + t);
  9. }
  10. }

Output result:

 
  1. The man who does good is C

Programming requirements

Carefully read the code framework and comments given in the editing area on the right, and write the program code according to the prompts.

Test description

The platform will use the test set to run the program code you write. If all the running results are correct, you will pass the customs. You can view the specific test set details in the "test results" area on the right.

Start your mission. I wish you success!

/*
Mission: China has four major freshwater lakes, namely Dongting Lake, Hongze Lake, Poyang Lake and Taihu Lake.
A Said: Dongting Lake is the largest, Hongze Lake is the smallest, and Poyang Lake is the third;
B Said: Hongze Lake is the largest, Dongting Lake is the smallest, Poyang Lake is the second and Taihu Lake is the third;
C Said: Hongze Lake is the smallest and Dongting Lake is the third;
D Said: Poyang Lake is the largest, Taihu Lake is the smallest, Hongze Lake is the second and Dongting Lake is the third.
Please output the correct ranking of 4 great lakes. Output style: Dongting Lake ranking: n 

A What you say can be expressed as follows:
int a1 = (dongting == 1) ? 1 : 0;//Dongting Lake is the largest (1). If yes, it is assigned as 1, otherwise it is assigned as 0
int a2 = (hongze == 4) ? 1 : 0;//Hongze Lake is the smallest (4). If yes, it is assigned as 1, otherwise it is assigned as 0
int a3 = (poyang == 3) ? 1 : 0;
a=a1+a2+a3;//A What you said

*/

public class LakeTest {
    public static void main(String[] args) {
        int a, b, c, d;   // Define what four people say
        int dongting, hongze, poyang, tai;  // Define 4 lakes
        
        // Please write code between begin and end
        /********** Begin **********/
        // Use the nested loop to traverse the four ranking situations of each lake in turn, express what everyone says with a logical expression, and output the specific ranking
       for(dongting=1;dongting<=4;dongting++){
           for(poyang=1;poyang<=4;poyang++){
               if(poyang==dongting) continue;
               for(hongze=1;hongze<=4;hongze++){
                   if(hongze==poyang||hongze==dongting) continue;
                   for(tai=1;tai<=4;tai++){
                       if(tai==hongze||tai==poyang||tai==dongting) continue;
                       a=(dongting==1?1:0)+(hongze==4?1:0)+(poyang==3?1:0);
                       b=(hongze==1?1:0)+(dongting==4?1:0)+(poyang==2?1:0)+(tai==3?1:0);
                       c=(hongze==4?1:0)+(dongting==3?1:0);
                       d=(poyang==1?1:0)+(tai==4?1:0)+(hongze==2?1:0)+(dongting==3?1:0);
                       if(a==1&&b==1&&c==1&&d==1){
                           System.out.println("Ranking of Dongting Lake:" + dongting);
                           System.out.println("Hongze Lake ranking:" + hongze);
                           System.out.println("Ranking of Poyang Lake:" + poyang);
                           System.out.println("Ranking of Taihu Lake:" + tai);
                       }
                   }
               }
           }
       }
       
       
        /********** End **********/

    }
}

Keywords: Java educoder

Added by dancingbear on Thu, 27 Jan 2022 21:52:09 +0200