Java Learning 10--loop structure while practice

Source: Internet
Author: User

Exercise 1:
get the and from 1 to 10, 10 digits. Master the idea of accumulating algorithm.
Exercise 2:
the number of times a multiple of 6 occurs between 1~100. Master counter ideas.

Class Whiletest {public static void main (string[] args) {/* Exercise 1: Get 1 to 10, 10 digits of the and.

        Master the idea of accumulating algorithm.
        Train of thought: 0+1 1+2 3+3 6+4 10+5 1. According to the above law, each participation in addition of the numerical uncertainty;
        2. Each occurrence and the data are also uncertain.

        3. It is found that the number of the participating addition operations is increasing; 4. Each time the wig is duplicated, and the next value is added.
        Steps: 1. Define a variable that records the data that participates in the addition operation.
        2. Define a variable to record each occurrence of the and.
        3. Increase the number of data participating in the addition operation.

        4. Because the addition operation needs to be repeated, it is necessary to think of the cyclic structure.

        * * SYSTEM.OUT.PRINTLN (1+2+3+4+5+6+7+8+9+10);
        Cumulative algorithm: The data is incremented.
            int x = 1;//record the data participating in addition; int sum = 0;//record each and while (x<=10) {//sum = sum + x;
            sum = x;            
        x + +;

        } System.out.println ("sum=" +sum);

        /* The number of times a multiple of 6 occurs between the practice 2:1~100.

        Train of thought: 1. From 1 to 100 to take out to judge, is not a multiple of 6, the value is changed; 2. The number of conditions to be satisfied after comparison is variable; 3. loop = traverse; Condition of traversal: The result of dividing each number by 6 is different. Step: 1. Define a variable to record the number of participating addition operationsAccording
        2. Define a variable to record the number of conditions that are met.
        3. To record data that participates in comparison operations.
        4. To meet the number of conditions to increase the self.

        5. Because the modular operation needs to be repeated, it is necessary to think of the cyclic structure.
            ///Counter thought int s = 1;//The value of the comparison is variable (incremented); int count = 0;//satisfaction is variable; while (s<=100) {             
            if (s%6==0) {count++;//System.out.println ("s=" +s);
        } s++;
    } System.out.println ("count=" +count);
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.