Java中的多个if条件

时间:2013-02-25 07:09:55

标签: android if-statement multiple-instances

我想知道如何用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。否则,ABNORMAL。

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

6 个答案:

答案 0 :(得分:2)

就像这样:

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

答案 1 :(得分:1)

if (60 <= heartrate <= 100)  

不会工作,因为你将布尔值与100进行比较60 <= hearrate返回True或False。,使用:

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

喜欢这样

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

答案 2 :(得分:0)

如果它有帮助,请尝试:

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
}

答案 3 :(得分:0)

你试过这个吗?

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

答案 4 :(得分:0)

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

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

布尔表达式可以是简单的测试,如5 > bsomeArray.isEmpty()true。另外,您可以使用AND &&,OR ||或NOT !之类的逻辑运算符来链接多个布尔值并将其评估为一个表达式。 E.g。

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

你真实的()留下未触动

If-then statements

答案 5 :(得分: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"); }