简体   繁体   English

有没有更好的方法来处理这些if语句?

[英]Is there a better way to process these if statements?

I'm writing a Java application that plots graphs for min/max/avg values of an accelerometer and gyroscope in their three fields xyz. 我正在编写一个Java应用程序,它在三个字段xyz中绘制加速度计和陀螺仪的最小/最大/平均值的图表。 I would like to be able to plot each factor independently, ie maximum acceleration in the X field with the average acceleration in the Z field etc. 我希望能够独立绘制每个因子,即X字段中的最大加速度与Z字段中的平均加速度等。

I have used radio buttons for min/max/avg and for acceleration/y/z and gyroscopex/y/z. 我已经使用了单选按钮来表示最小/最大/平均值以及加速/ y / z和陀螺仪x / y / z。

I also thought about making radio buttons for min/max/avg for each variable but then I would have 18 separate variables. 我还考虑过为每个变量设置最小/最大/平均的单选按钮,但是那时我将有18个单独的变量。

Here is a snippet of my code: 这是我的代码片段:

if (selectionAccelX == 1 && selectionAccelY == 0 && selectionAccelZ == 0 && 
    selectionGyroX == 0 && selectionGyroY == 0 && selectionGyroZ == 0 && 
    selectionXAxis == 0) {

    Graphing graphing = new Graphing("Average AccelerationX");
    graphing.setAccelerationX(avgAccXSec);
    graphing.displayGraphing();
    graphing.pack();
    RefineryUtilities.centerFrameOnScreen(graphing);
    graphing.setVisible(true);
}

Is there an easier way to go about this? 有没有更简单的方法可以做到这一点?

Your code maps one variable (the radio button selector) to another variable (the value selector). 您的代码将一个变量(单选按钮选择器)映射到另一个变量(值选择器)。 One possible solution can be to 一种可能的解决方案是

  • add an enum (with 18 values again) 添加一个枚举(再次具有18个值)
  • bind every value of the enum to the return value of the radio button 将枚举的每个值绑定到单选按钮的返回值
  • add an switch/case based on the return value to trigger the right behaviour 根据返回值添加开关/案例以触发正确的行为

I assume that is not lesser code but can be more readable. 我认为这不是更少的代码,而是更具可读性。

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

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