1

我想知道如何在 Java 中编写多个条件。

for (int i = 1; i <= pqrst; i++) {
    P = P + "{" + Pxcoor[i] + "," + Pycoor[i] + "} ; ";
    Q = Q + "{" + Qxcoor[i] + "," + Qycoor[i] + "} ; ";
    R1 = R1 + "{" + Rxcoor[i] + "," + Rycoor[i] + "} ; ";
    S = S + "{" + Sxcoor[i] + "," + Sycoor[i] + "} ; ";
    T = T + "{" + Txcoor[i] + "," + Tycoor[i] + "} ; ";


    if (i >= 2) {
        rrdistance += (((Rxcoor[i] - Rxcoor[i - 1]) / FINALBOXWIDTH(bitmap_Source)) * 0.04);//durations in seconds
        printerval += (((Rxcoor[i] - Pxcoor[i]) / FINALBOXWIDTH(bitmap_Source)) * 0.04);
        qsdistance += (((Sxcoor[i] - Qxcoor[i]) / FINALBOXWIDTH(bitmap_Source)) * 0.04);
        heartrate += (1500 / (rrdistance / 0.04)); 

    //amplitude in mV
        pamp = (( (trial[1] - Pycoor[i]) / (double)FINALBOXWIDTH(bitmap_Source))  * 0.1);
        qamp = (( (Qycoor[i] - trial[i]) / (double)FINALBOXWIDTH(bitmap_Source)) *0.1);
        ramp = (( (trial[1] - Rycoor[i]) / (double)FINALBOXWIDTH(bitmap_Source)) *0.1);
        samp = (( (Sycoor[i] - trial[i]) / (double)FINALBOXWIDTH(bitmap_Source)) *0.1);
        tamp = (( (trial[1] - Tycoor[i]) / (double)FINALBOXWIDTH(bitmap_Source)) *0.1);


                        }
                    }
DecimalFormat df = new DecimalFormat("#.##");
rrdistance = rrdistance / (pqrst - 1);
RRdistance = RRdistance + " " + rrdistance + " " + "seconds";
PRinterval = PRinterval + " " + printerval + " " + "seconds";
QSdistance = QSdistance + " " + qsdistance + " " + "seconds";
HeartRate = HeartRate + " " + heartrate + " " + "bpm";

Pamp = Pamp + " " + df.format(pamp) + " " + "mV";
Qamp = Qamp + " " + df.format(qamp) + " " + "mV";
Ramp = Ramp + " " + df.format(ramp) + " " + "mV";
Samp = Samp + " " + df.format(samp) + " " + "mV";
Tamp = Tamp + " " + df.format(tamp) + " " + "mV";

coordinate.setText("Peak coordinates:" + "\n" + P + "\n" + Q + "\n" + R1 + "\n" + S
+ "\n" + T + "\n" + RRdistance + "\n" + PRinterval + "\n" + QSdistance + "\n"
+ HeartRate + " "+ "\n" + "Amplitude of peaks:" + "\n" + Pamp + ";"  + " " + Qamp + ";"     + " "  + Ramp + ";" + " "  + Samp + ";" + " "  + Tamp + ";");

我将使用 heartrate、pamp、printerval 和 qsdistance。如何将所有这些条件写在一个 if 语句中?

if (60 <= heartrate <= 100) && (0 <= pamp <= 0.25) && (0.12 <= printerval <= 0.20) && (0.04 <= qsdistance < 0.12)

如果所有这些条件都满足,那么它将显示 NORMAL。否则,异常。

我尝试将所有这些都放在一个 if 语句中,但它显示错误。我希望你能帮助我弄清楚如何做到这一点。谢谢!

4

6 回答 6

2
if (60 <= heartrate <= 100)  

wont work because you compare a boolean to 100 The 60 <= hearrate returns True or False., use :

if (heartrate >=60 && heartrate <=100)

like so

if ((heartrate >= 60 && heartrate <= 100) && (pamp >= 0 && pamp <= 0.25) && (printerval >= 0.12  &&printerval <= 0.20) && (qdistance >= 0.04 && qsdistance < 0.12)){
}
于 2013-02-25T07:13:23.427 回答
2

It would be like this:

 if ((heartrate >= 60) && (heartrate <= 100) && (pamp >=0) && (pamp <= 0.25) && (printerval >= 0.12) && (printerval <= 0.20) && (qsdistance >= 0.04) && (qsdistance < 0.12))
于 2013-02-25T07:14:07.257 回答
0

Try it if it helps:

if ((60 <= heartrate) && (heartrate <= 100) && (0 <= pamp) && (pamp <= 0.25) && (0.12 <= printerval) 
&& (printerval <= 0.20) && (0.04 <= qsdistance) && (qsdistance < 0.12)) {

  //Display NORMAL Here(use textview or Log whatever you want)
 } else {
 //Abnormal
}
于 2013-02-25T07:14:28.137 回答
0

我实际上有点害怕看到三个答案不遵循if声明的 Java 语法,所以让我们澄清一下。

if( [boolean expresison] ){
  [then block]
}

一个布尔表达式可以是一个简单的测试,比如5 > bsomeArray.isEmpty()或者只是true。另外,您可以使用 AND &&、 OR||或 NOT等逻辑运算符!链接多个布尔值并将其计算为一个表达式。例如

if( foo && bar || !quix){
  return true;
}

你看到不断变化的()保持不变

If-then 语句

于 2013-02-25T07:25:07.470 回答
0
if ((heartrate >=60 && heartrate <=100) && 
                (pamp >= 0 && pamp <= 0.25) && 
                (printerval >= 0.12 && printerval <= 0.20) &&
                (qsdistance >= 0.04 && qsdistance <= 0.04)) {
            System.out.println("NORMAL");

        } else { System.out.println("ABNORMAL"); }
于 2013-02-25T07:25:17.727 回答
0

你试过这个吗?

if ((60 <= heartrate && heartrate <= 100) && (0 <= pamp && pamp <= 0.25) && (0.12 <= printerval && printerval <= 0.20) && (0.04 <= qsdistance && qsdistance < 0.12))
于 2013-02-25T07:14:34.103 回答