-3

Problem while entering a 3 digit no and getting no result. after entering 123 and when 2 is detected as even, and if user enters 7, it should display 173. but the program is immediately ending. It might be a problem in the last 0 check if-block. but removing it also doesn't help. Thanks in advance!

// in 3 dig, check even dig., replace them with odd and disp.
import java.util.*;
public class p24123 {
    public static void main(String[] args) {
        int n,h,t,o,m,z=0,z1=0,z2=0,fn;
        Scanner ob = new Scanner(System.in);
        n=ob.nextInt();
        if(n>99&&n<1000){
            h= n/100;
            o=n%10;
            m=n/10;
            t=m%10;
            if(h%2==0){
               z=h;
                System.out.println("Enter the odd number you would like to replace the EVEN hundred's digit"+h+" with \n");
                z=ob.nextInt();
                if(z%2==0){
                    System.out.println("That's not odd. So we will keep the original digit in it's place");
                    z=h;
                }
                else if(t%2==0) {
                    System.out.println("Condition enter bokachpda");
                    z1 = t;
                    System.out.println("Enter the odd number you would like to replace the EVEN ten's digit" + t + " with \n");
                    z1 = ob.nextInt();
                    if (z % 2 == 0) {
                        System.out.println("That's not odd. So we will keep the original digit in it's place");
                        z1 = t;
                    }
                }
                else if(o%2==0){
                    z2=o;
                    System.out.println("Enter the odd number you would like to replace the EVEN one's digit"+h+" with \n");
                    z2=ob.nextInt();
                    if(z2%2==0){
                        System.out.println("That's not odd. So we will keep the original digit in it's place");
                        z2=o;
                    }
                }
                else if(2==2){
                    if(h<1||t<1||o<1||z<1||z1<1||z2<1){
                        System.out.println("Error");
                        System.exit(0);

                    }
                }
                fn=z*100+z1*10+z;


            }
        }
    }

}

  • Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Jan 25 '23 at 18:10

1 Answers1

0

Here's your code cleaned up and fixed. I modified as little as possible to keep it at a level a beginner would be comfortable with. Some improvements to be made:

  • Repeated code like this screams, "Put me in my own function!"
  • A loop can be used to handle any number of digits, not just three.
  • Error checking/handling. You should handle bad input. What if the user enters "hello" instead of a number?

Improvements I made:

  • Your original code never printed a result.
  • Better formatting. It makes the code easier to read.
  • Descriptive variable names!
Scanner ob = new Scanner(System.in);
int n = ob.nextInt();
if (n > 99 && n < 1000) {
    int hundredsDigit = n / 100;
    int tensDigit = n / 10 % 10;
    int onesDigit = n % 10;
    
    if (hundredsDigit % 2 == 0) {
        System.out.println("Enter the odd number you would like to replace the EVEN hundred's digit " + hundredsDigit +" with \n");
        int replacementDigit = ob.nextInt();
        if (replacementDigit % 2 == 0) {
            System.out.println("That's not odd. So we will keep the original digit in it's place");
        }
        else {
            hundredsDigit = replacementDigit;
        }
    }
    if (tensDigit % 2 == 0) {
        System.out.println("Enter the odd number you would like to replace the EVEN ten's digit " + tensDigit + " with \n");
        int replacementDigit = ob.nextInt();
        if (replacementDigit % 2 == 0) {
            System.out.println("That's not odd. So we will keep the original digit in it's place");
        }
        else {
            tensDigit = replacementDigit;
        }
    }
    if (onesDigit % 2 == 0) {
        System.out.println("Enter the odd number you would like to replace the EVEN one's digit " + onesDigit + " with \n");
        int replacementDigit = ob.nextInt();
        if (replacementDigit % 2 == 0) {
            System.out.println("That's not odd. So we will keep the original digit in it's place");
        }
        else {
            onesDigit = replacementDigit;
        }
    }
    System.out.println(hundredsDigit * 100 + tensDigit * 10 + onesDigit);
}
001
  • 13,291
  • 5
  • 35
  • 66