Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner
- public Project(){
- static int odd, even, zero, sum;
- public static void main(String[] args){
- Scanner scan = new Scanner(System.in);
- odd = 0;
- even = 0;
- zero = 0;
- sum = 0;
- int command;
- int newInt;
- //Get default int
- getInt();
- do{
- System.out.println("1. Get new int\n" +
- "2. Display how many odd digits, even digits, and zeros are in the int\n" +
- "3. Get the sum of the digits in the int\n" +
- "4. Exit program\n");
- command = scan.nextInt();
- if(command > 0 && command < 5){
- switch(command){
- case 1:
- getInt();
- break;
- case 2:
- System.out.println("odd digits: " + odd);
- System.out.println("even digits: " + even);
- System.out.println("zero digits: " + zero);
- break;
- case 3:
- System.out.println("sum of digits: " + sum);
- break;
- case 4:
- System.out.println("Exiting");
- break;
- default:
- System.out.println("Invalid Command");
- break;
- }
- }while(command != 4);
- }
- public static void getInt(){
- System.out.println("Please enter the integer:");
- newInt = scan.nextInt();
- //reset all values
- odd = 0;
- even = 0;
- zero = 0;
- sum = 0;
- //Get digits of the number
- int[] digits = new int[((Int)Math.log10(newInt)) + 1];
- for(int i = 0; i < comp.size; i++){
- digits[i] = newInt%10;
- newInt = newInt/10;
- }
- //Computing # of odd, even, zero digits as well as the sum
- for(int i = 0; i < comp.size; i++){
- //odd, even and zero digits
- if(digits[i]%2 == 1) odd++;
- else if(digits[i] == 0) zero++;
- else even++;
- sum += digits[i];
- }
- }//end getInt
- }
Add Comment
Please, Sign In to add comment