简体   繁体   English

程序中输入 3 位数字并检查哪些数字是偶数并将其替换为奇数的问题

[英]Problem in program to input a 3 digit number and check which digits are even and replace it with odd digits

Problem while entering a 3 digit no and getting no result.输入 3 位数编号时出现问题,但没有得到任何结果。 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.输入 123 后,当检测到 2 为偶数时,如果用户输入 7,则应显示 173。但程序立即结束。 It might be a problem in the last 0 check if-block.这可能是最后一个 0 检查 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;


            }
        }
    }

}

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?如果用户输入“hello”而不是数字怎么办?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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