java 更好的技术在这?

xqnpmsa8  于 2023-03-28  发布在  Java
关注(0)|答案(3)|浏览(118)

我有一个Java程序,我使用Eclipse。我的程序接受3个值,并有3个预期值,它决定哪些值超出公差。我需要打印哪些值超出公差。它可能只是值1,所有这些,等等。现在我有一堆else if语句来覆盖所有超出公差的值组合。有没有更好的方法来做到这一点?谢谢!
这个程序按现在的方式运行得很好,只是看起来像是坏代码。
当前代码:

if (thicknessIsGood == true && widthIsGood == true && lengthIsGood == true) {
        lumberReport = "Lumber dimensions meet tolerance standards of 0.03125 inches";
    }
    //check if all dimensions are within tolerance
    else if (thicknessIsGood == false && widthIsGood == false && lengthIsGood == false) {
        lumberReport = "Lumber dimensions are not within tolerance. All dimensions exceed tolerance of 0.03125 inches.";
    }
    //check if thickness is bad
    else if (thicknessIsGood == false && widthIsGood == true && lengthIsGood == true) {
        lumberReport = "Lumber dimensions are not within tolerance. Thickness exceeds tolerance of 0.03125 inches.";
    }
    //check if width is bad
    else if (thicknessIsGood == true && widthIsGood == false && lengthIsGood == true) {
        lumberReport = "Lumber dimensions are not within tolerance. Width exceeds tolerance of 0.03125 inches.";
    }
    //check if length is bad
    else if (thicknessIsGood == true && widthIsGood == true && lengthIsGood == false) {
        lumberReport = "Lumber dimensions are not within tolerance. Length exceeds tolerance of 0.03125 inches.";
    }
    //to cover all bases in lieu of 6 more else if statements (if this is wrong, please let me know
    else { 
        lumberReport = "Lumber dimensions are not within tolerance. Two dimensions exceed tolerance of 0.03125 inches.";
    }
hgtggwj0

hgtggwj01#

稍微重新排列一下,您就可以报告哪些维度没有得到满足。

  • 首先将消息存储在数组中。
  • 然后将索引设置为0。
  • 如果宽度不好,则设置为1
  • 如果长度不好,则加2
  • 如果厚度不好,则增加4。

以下是指数的总结。

// goodWidth + goodLength + goodthickness == 0
// badWidth  + goodLength + goodthickness == 1
// goodWidth + badLength  + goodthickness == 2
// badWidth  + badLength  + goodthickness == 3
// goodWidth + goodLength + badthickness  == 4
// badWidth  + goodLength + badthickness  == 5
// goodWidth + badLength  + badthickness  == 6
// badWidth  + badLength  + badthickness  == 7

您有八条消息,不同组合的值将为消息创建适当的索引并返回它。

private static String[] msgs = {
         "Lumber dimensions meet tolerance standards of 0.03125 inches",
         "Lumber dimensions are not within tolerance.\nWidth exceeds tolerance of 0.03125 inches.",
         "Lumber dimensions are not within tolerance.\nLength exceeds tolerance of 0.03125 inches.",
         "Lumber dimensions are not within tolerance.\nWidth and length exceeds tolerance of 0.03125 inches.",
         "Lumber dimensions are not within tolerance.\nThickness exceeds tolerance of 0.03125 inches.",
         "Lumber dimensions are not within tolerance.\nWidth and thickness exceeds tolerance of 0.03125 inches.",
         "Lumber dimensions are not within tolerance.\nLength and thickness exceeds tolerance of 0.03125 inches.",
         "Lumber dimensions are not within tolerance.\nAll dimensions exceed tolerance of 0.03125 inches."};

System.out.println(getMessage(true, false, false));

印刷品

Lumber dimensions are not within tolerance.
Length and thickness exceeds tolerance of 0.03125 inches.

该方法

public static String getMessage(boolean widthIsGood, boolean lengthIsGood, boolean thicknessIsGood) {
    int index = widthIsGood ? 0 : 1;
    if (!lengthIsGood) {
        index +=2;
    }
    if(!thicknessIsGood) {
        index += 4;
    }
}
9w11ddsr

9w11ddsr2#

这不会产生完全相同的输出,但您可以报告不符合标准的每个尺寸。

if (thicknessIsGood && widthIsGood && lengthIsGood) {
    lumberReport = "Lumber dimensions meet tolerance standards of 0.03125 inches";
} else {
    lumberReport = "Lumber dimensions are not within tolerance.";
    if (!thicknessIsGood) lumberReport += " Thickness exceeds tolerance of 0.03125 inches.";
    if (!widthIsGood) lumberReport += " Width exceeds tolerance of 0.03125 inches.";
    if (!lengthIsGood) lumberReport += " Length exceeds tolerance of 0.03125 inches.";
}
e4yzc0pl

e4yzc0pl3#

我将使用一个数字作为位图,其中1和0分别代表truefalse,以及开关情况下的相应常数:

int mask = ( thicknessIsGood ? 4 : 0 ) | ( widthIsGood ? 2 : 0 ) | ( lengthIsGood ? 1 : 0 );

switch( mask ){
  case 7: // 111 binary
   lumberReport = "Lumber dimensions meet tolerance standards of 0.03125 inches";
   break;

  case 0: // 000 bin
    lumberReport = "Lumber dimensions are not within tolerance. All dimensions exceed tolerance of 0.03125 inches.";
    break;
  
  case 3: // 011 bin
    lumberReport = "Lumber dimensions are not within tolerance. Thickness exceeds tolerance of 0.03125 inches.";
    break;

  // other cases

  default:
    lumberReport = "Lumber dimensions are not within tolerance. Two dimensions exceed tolerance of 0.03125 inches.";   
}

相关问题