Java method to determine the number of digits, java method to determine the number of digits

Source: Internet
Author: User

Java method to determine the number of digits, java method to determine the number of digits

Common method:

Import java. util. iterator; public class Digits {public static void main (String [] args) {iterator input = new iterator (System. in); // declare the scanner variable System. out. println ("enter an integer 0-999999999"); // The system prompts you to enter try {// listen for an exception while (true) {int num = input. nextInt (); int count = 0; if (num <0 | num> 999999999) System. out. println ("input out of range"); else if (num = 0) System. out. println ("1-digit input"); else {while (num> 0) {num = num/10; count ++;} System. out. println ("the input is" + count + "bits") ;}} catch (Exception e) {// catch the Exception System. out. println ("Enter it correctly"); e. printStackTrace (); // print the location and cause of the error in the program }}}

General functions/methods:

Import java. util. iterator; public class Digits {boolean digits (int num) {// create a boolean type method if (num <0 | num> 999999999) {return true ;} else {return false ;}} public static void main (String [] args) {Digits d = new Digits (); // create the object metadata input = new partition (System. in); // declare the scanner variable System. out. println ("enter an integer 0-999999999"); // The system prompts you to enter try {// listen for an exception while (true) {int num = input. nextInt (); // obtain the value int count = 0 in the next row; if (num = 0) {System. out. println ("1 digit entered");} else if (d. digits (num) {// call the digits method System. out. println ("input out of range");} else {while (num> 0) {num = num/10; count ++;} System. out. println ("the input is" + count + "bits") ;}} catch (Exception e) {// catch the Exception System. out. println ("Enter it correctly"); e. printStackTrace (); // print the location and cause of the error in the program }}}

Note: method 2 uses the object-oriented idea

Related Article

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.