java循环、偶数和零

java循环、偶数和零,java,loops,Java,Loops,我目前正在开发一个程序,可以计算用户输入数字中的偶数、赔率和零。我在两件事上有困难。一个是我不知道如何让循环继续,直到输入哨兵号,另一个是我如何将零计算为零而不是偶数。下面是我目前的代码,谢谢大家 import java.util.Scanner; public class CountDigitsWithSentinel { public static void main(String[] args) { // Declare the identifier

我目前正在开发一个程序,可以计算用户输入数字中的偶数、赔率和零。我在两件事上有困难。一个是我不知道如何让循环继续,直到输入哨兵号,另一个是我如何将零计算为零而不是偶数。下面是我目前的代码,谢谢大家

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!");
    
}
}
如果(rem==0),您可以添加
,但我建议您使用字符串,因为如果用户输入的数字以0开头,程序将不计算它:

带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!");
    
}
}
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!
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!");
    
}
}
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!
带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!");
    
}
}
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!
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!");
    
}
}
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!
带字符串:

 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!");
    
}
}
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!
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!");
    
}
}
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!
带字符串的输出:

 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!");
    
}
}
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!
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!");
    
}
}
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!

我可能会将其转换为字符串,然后在switch语句中使用正则表达式对其求值。如果缓存(预定义)reg EXE,这也会非常有效,因为不必先将每个字符转换为整数。

什么是输入重设?可能只需将其转换为字符串,然后通过字符串字符迭代,每个字符转换为数字,然后检查是否为事件,奇数或零范围可以是除-99之外的任何整数,它需要是哨兵号。