繁体   English   中英

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

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

输入 3 位数编号时出现问题,但没有得到任何结果。 输入 123 后,当检测到 2 为偶数时,如果用户输入 7,则应显示 173。但程序立即结束。 这可能是最后一个 0 检查 if-block 的问题。 但删除它也无济于事。 提前致谢!

// 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;


            }
        }
    }

}

这是您清理并修复的代码。 我尽可能少地修改以将其保持在初学者会感到舒服的水平。 需要做的一些改进:

  • 像这样重复的代码尖叫着,“把我放在我自己的函数中!”
  • 循环可用于处理任意数量的数字,而不仅仅是三个。
  • 错误检查/处理。 你应该处理错误的输入。 如果用户输入“hello”而不是数字怎么办?

我所做的改进:

  • 您的原始代码从未打印出结果。
  • 更好的格式化。 它使代码更易于阅读。
  • 描述性变量名!
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