更好的技术?

问题描述 投票:0回答:3

我有一个 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.";
    }
java if-statement combinations
3个回答
0
投票

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

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.";
}

0
投票

我会使用一个数字作为位图,其中 1 和 0 分别代表

true
false
以及 switch-case 中的相应常量:

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.";   
}

0
投票

稍微重新排列一下,您就可以报告不符合哪些维度。

  • 首先将消息存储在数组中。
  • 然后将索引设置为 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 = 0;
    if (!widthIsGood) {
        index = 1;
    }
    if (!lengthIsGood) {
        index += 2;
    }
    if (!thicknessIsGood) {
        index += 4;
    }
    return msgs[index];
}
© www.soinside.com 2019 - 2024. All rights reserved.