Guest User

Untitled

a guest
Jun 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. import java.util.Scanner
  2. public Project(){
  3.  
  4.     static int odd, even, zero, sum;
  5.  
  6.     public static void main(String[] args){
  7.        
  8.         Scanner scan = new Scanner(System.in);
  9.         odd = 0;
  10.         even = 0;
  11.         zero = 0;
  12.         sum = 0;
  13.  
  14.         int command;
  15.         int newInt;
  16.  
  17.         //Get default int
  18.         getInt();
  19.  
  20.  
  21.         do{
  22.             System.out.println("1.  Get new int\n" +
  23.             "2. Display how many odd digits, even digits, and zeros are in the int\n" +
  24.             "3. Get the sum of the digits in the int\n" +
  25.             "4. Exit program\n");
  26.            
  27.             command = scan.nextInt();
  28.             if(command > 0 && command < 5){
  29.             switch(command){
  30.  
  31.             case 1:
  32.                 getInt();
  33.             break;
  34.  
  35.  
  36.             case 2:
  37.             System.out.println("odd digits: " + odd);
  38.             System.out.println("even digits: " + even);
  39.             System.out.println("zero digits: " + zero);
  40.             break;
  41.            
  42.  
  43.             case 3:
  44.             System.out.println("sum of digits: " + sum);
  45.             break;
  46.  
  47.  
  48.             case 4:
  49.             System.out.println("Exiting");
  50.             break;
  51.  
  52.             default:
  53.             System.out.println("Invalid Command");
  54.             break;
  55.  
  56.             }
  57.         }while(command != 4);
  58.     }
  59.  
  60.  
  61.     public static void getInt(){
  62.             System.out.println("Please enter the integer:");
  63.             newInt = scan.nextInt();
  64.                 //reset all values
  65.                 odd = 0;
  66.                 even = 0;
  67.                 zero = 0;
  68.                 sum = 0;
  69.            
  70.             //Get digits of the number
  71.             int[] digits = new int[((Int)Math.log10(newInt)) + 1];
  72.                 for(int i = 0; i < comp.size; i++){
  73.                     digits[i] = newInt%10;
  74.                     newInt = newInt/10;
  75.                 }
  76.  
  77.             //Computing # of odd, even, zero digits as well as the sum
  78.                 for(int i = 0; i < comp.size; i++){
  79.  
  80.                     //odd, even and zero digits
  81.                     if(digits[i]%2 == 1) odd++;
  82.                     else if(digits[i] == 0) zero++;
  83.                     else even++;
  84.  
  85.                     sum += digits[i];
  86.                 }
  87.     }//end getInt
  88.  
  89. }
Add Comment
Please, Sign In to add comment