dm6801

20171113 - 03 - classEx_04

Nov 13th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.75 KB | None | 0 0
  1. package myApp;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class class03_ex10 {
  6.  
  7.     public static void main(String[] args) {
  8.         //init vars
  9.         final int   TIMES = 3;
  10.         final int   MOD = 3;
  11.        
  12.         int         n = 0,
  13.                     nLength = 0;
  14.        
  15.         //open in_stream
  16.         Scanner s = new Scanner(System.in);
  17.        
  18.         //usage info
  19.         System.out.printf("Type in %d digits: ", TIMES);
  20.        
  21.         //nLength holds n's digit count
  22.         while (nLength != TIMES || (n % MOD != 0)) {
  23.             nLength = 0; //reset counter from last iteration
  24.             n = s.nextInt(); //accept new n value
  25.            
  26.             //calc digit count
  27.             for (int i=n; i>0; i /= 10) {
  28.                 nLength += 1;
  29.             }
  30.            
  31.         }
  32.                
  33.         //close in_stream
  34.         s.close();
  35.        
  36.         //exit msg
  37.         System.out.printf("number %d accepted. exit..%n", n);
  38.        
  39.     }
  40.  
  41. }
Advertisement
Add Comment
Please, Sign In to add comment