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

xzlaal3s  于 2023-01-29  发布在  Java
关注(0)|答案(1)|浏览(138)

输入3位数字no时出现问题,没有结果。输入123后,当2被检测为偶数时,如果用户输入7,则应显示173。但程序立即结束。这可能是最后一个0检查if块中的问题。但删除它也没有帮助。提前感谢!

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

            }
        }
    }

}
u1ehiz5o

u1ehiz5o1#

这是你的代码清理和修复。我修改尽可能少,以保持它在一个初学者将舒适的水平。一些改进:

  • 像这样重复的代码尖叫着:“把我放到我自己的函数中!”
  • 循环可用于处理任意数量的数字,而不仅仅是三位。
  • 错误检查/处理。你应该处理错误的输入。如果用户输入“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);
}

相关问题