简体   繁体   中英

java loops, evens odds and zeros

I am currently working on a program that counts the number of even, odds, and zeros in an user input number. I am having trouble with two things. One being I am not sure how to get the loop to continue until the sentinel number is entered, and the other is how do I count zeros as a zero and not an even number. Below is my code so far, Thank you in advance.

import java.util.Scanner;

public class CountDigitsWithSentinel
{
    
    public static void main(String[] args)
    {
      // Declare the identifiers
        final int SENTINEL = -99;
        int userInput;
        int oddNums = 0;
        int evenNums = 0;
        int numZeros = 0;
        // Declare the remaining identifiers ...

        Scanner scan = new Scanner(System.in);
        
        // Display the programmer's information
        
         
        
      System.out.println("Enter an integer value (-99 to end):");
       userInput = scan.nextInt();
        System.out.println("The number " + userInput + " contains");
        //Your code ...
       
        
        
         while (userInput > 0) {
            
            int rem = userInput % 10;
            
            
            if (rem% 2 == 0) {
               evenNums++;
            
         }
         
         else
         {
            oddNums++;
         }
            
            userInput = userInput / 10;
         }
        
        //Your code ...
        
        // Display the counts
      System.out.println("Zero digits: " + numZeros);
      System.out.println("Even digits: " + evenNums);
      System.out.println("Odd digits: " + oddNums);
        //Your code ...
         
        System.out.println("Have a nice day!");
    
}
}

You can add if(rem == 0) but I suggest you work with a string because if the number the user enters starts with 0, the program will not count it:

With int :

 public static void main(String[] args)
    {
      // Declare the identifiers
        final int SENTINEL = -99;
        int userInput,oddNums = 0,evenNums = 0,numZeros = 0;
        // Declare the remaining identifiers ...

        Scanner scan = new Scanner(System.in);
        
        // Display the programmer's information
        
         
        
      System.out.println("Enter an integer value (-99 to end):");
       userInput = scan.nextInt();
        System.out.println("The number " + userInput + " contains");
        //Your code ...
       
        
        
         while (userInput > 0) {
            
            int rem = userInput % 10;
            
            
            if (rem% 2 == 0) {
               evenNums++;
            
         }
         
         else
         {
            oddNums++;
         }
            if(rem == 0) {
                numZeros++;
            }
            
            userInput = userInput / 10;
         }
        
        //Your code ...
        
        // Display the counts
      System.out.println("Zero digits: " + numZeros);
      System.out.println("Even digits: " + evenNums);
      System.out.println("Odd digits: " + oddNums);
        //Your code ...
         
        System.out.println("Have a nice day!");
    
}
}

Output with int:

Enter an integer value (-99 to end):
054740102
The number 54740102 contains
Zero digits: 2
Even digits: 5
Odd digits: 3
Have a nice day!

With String :

public static void main(String[] args)
    {
      // Declare the identifiers
        final int SENTINEL = -99;
        String userInput = "";
        int oddNums = 0,evenNums = 0,numZeros = 0;
        // Declare the remaining identifiers ...

        Scanner scan = new Scanner(System.in);
        
        // Display the programmer's information
        
         
        
      System.out.println("Enter an integer value (-99 to end):");
       userInput = scan.nextLine();
        System.out.println("The number " + userInput + " contains");
        //Your code ...
       
        
        
         while (Integer.parseInt(userInput) > 0) {
            
            int rem = Integer.parseInt(userInput) % 10;
            
            
            if (rem% 2 == 0) {
               evenNums++;
            
         }
         
         else
         {
            oddNums++;
         }
            if(rem == 0) {
                numZeros++;
            }
            
            userInput = userInput.substring(0, userInput.length() - 1);
         }
        
        //Your code ...
        
        // Display the counts
      System.out.println("Zero digits: " + numZeros);
      System.out.println("Even digits: " + evenNums);
      System.out.println("Odd digits: " + oddNums);
        //Your code ...
         
        System.out.println("Have a nice day!");
    
}
}

Output with string:

Enter an integer value (-99 to end):
054740102
The number 054740102 contains
Zero digits: 2
Even digits: 5
Odd digits: 3
Have a nice day!

I would probably cast it to a string and then evaluate it with regular expressions in a switch statement. If you cache (predefine) the reg-exes this would also be rather efficient because it wouldnt be necessary to convert each char to an int first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM