简体   繁体   中英

How to check for odd number of digits in Java?

I need to write a program that accepts the following conditions:

  1. It has to be an odd number,
  2. Have an odd number of digits,
  3. All of its digits have to be odd numbers and
  4. The number has to be between 101 and 1000001.

I am currently stuck on trying to check if it has an odd number of digits. Any help would be appreciated. Update: Thanks for all the help everyone. It was really helpful!

import java.util.Scanner;
public class TestOddNumbers {

public static void main(String[] args) {

    Scanner stdin = new Scanner(System.in);
    int userInput;
    final int EXIT = -1;
    final int MIN = 101;
    final int MAX = 1000001;
    do{
        System.out.println("please enter a positive whole number between"
                + "" + MIN + " and " + MAX + ". Enter " + EXIT + " "
                + "when you are done entering numbers.");
        userInput = stdin.nextInt();      
    }   
    while(userInput != EXIT && userInput >= MIN && userInput <= MAX); 
    if(userInput % 2 == 1){  
          System.out.println("This is an odd number");
        } 
        else{
            System.out.println("This is not an odd number");
        }
      }  
   }        
}

How about this ?

This psuedocode is not a solution or an attempt to a solution.

//this should probably be outside and before your while loop.
int length = Integer.toString(userInput).length();
if (length % 2 == 0) {
  return false; //even number of digits
}

...

//this should be inside your while loop
while(userInput != 0) {
  remainder = userInput % 10;
  userInput = userInput / 10;
  if (remainder % 2 != 0) {
    return false; //One of the digit is even
  }
  //if every digit is odd, it has to be an odd number, so checking if origina userInput is an odd number is not necessary. 
}

Take this pseudo-code as the starting point.

try passing the value of userInput into a string and check its length if it is odd or not.

String total = "";

do{
        System.out.println("please enter a positive whole number between"
                + "" + MIN + " and " + MAX + ". Enter " + EXIT + " "
                + "when you are done entering numbers.");
        userInput = stdin.nextInt();
        total += String.valueOf(userInput);
    }   
    while(userInput != EXIT && userInput >= MIN && userInput <= MAX);
    System.out.println(total);
    if(total.length() % 2 == 1){  
          System.out.println("This is an odd number");
        } 
        else{
            System.out.println("This is not an odd number");
        }

This piece of code will check if each digit is odd and also if length is also odd.

    int test = 6789;
    int a = test;
    int length =0;
    boolean hasEachDigitOdd = true;
    while (a!= 0) {
        int remaider = a % 10;
        if (remaider % 2 == 0) {
            hasEachDigitOdd = false;
        }
        a = a / 10;
        length++;
    }
    System.out.println(length);
    System.out.println(hasEachDigitOdd);
if (num >= 101 && num <= 1000001 && num % 2 == 0)

百分号为您提供除法运算的其余部分。

Please check below - the solution that meets all your needs. Even the way you took input needed to be fixed.

public static void main(String[] args) {
    Scanner stdin = new Scanner(System.in);
    int userInput;
    final int EXIT = -1;
    final int MIN = 101;
    final int MAX = 1000001;
    do {
        System.out.println("please enter a positive whole number between"
                + "" + MIN + " and " + MAX + ". Enter " + EXIT + " "
                + "when you are done entering numbers.");
        userInput = stdin.nextInt();

        if(userInput==EXIT){
            System.out.println("done");
            break;
        }

        if (userInput % 2 == 1) {
            System.out.println("This is an odd number");
        } else {
            System.out.println("This is not an odd number");
        }

        if(String.valueOf(userInput).length()%2==1){
            System.out.println("Has odd number of digits");
        } else {
            System.out.println("Has even number of digits");
        }
        boolean[] allOdds = {true};

        String.valueOf(userInput).chars().forEach(i -> {
            if(Integer.parseInt(String.valueOf((char)i))%2==0){
                allOdds[0] = false;
            }
        });
        if(allOdds[0]){
            System.out.println("all digits are odds");
        }else{
            System.out.println("all digits are not odds");
        }

    } while (userInput >= MIN && userInput <= MAX);
}

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