In java, the return value is 3 in-, and the number of digits is 6, and the value is -.

Source: Internet
Author: User

In java, the return value is 3 in-, and the number of digits is 6, and the value is -.

/* The number of integers in 1000-9999 that can be divided by 3 and the single digit is 6 */
Public class IntTest {
Public static void main (String [] args ){

// Instantiate an object
IntTest it = new IntTest ();

// Output the returned data
System. out. println (it. zb (1000,999 9 ));
}

Public int zb (int start, int end ){
// Declare a counting variable. The initial value is 0.
Int flag = 0;
For (int I = start; I <= end; I + = 2 ){
// As long as it is divided by 3, the single digit is 6. Add 1 to the counter
If (I % 3 = 0 & I % 10 = 6 ){
Flag ++;
}
}
// Return the calculated value
Return flag;
}

Resolution: I + = 2; or I + = 1; I + = 2 is used because a single position of 6 is certainly divisible by 2. Reduce the number of cycles.

If you have any good methods or do not understand them, you are welcome to comment on them.

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.