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