2010-07-18 19 views
2

我只需要使用布爾和if語句來確定數字是否在141和185之間,以及它是低於還是高於該數字。我很難過。使用布爾代數來確定兩個數字之間的限制?

double maxHR= 220- Double.parseDouble(yearsOld);  //maximum heart rate 
double reserveHR= maxHR- Double.parseDouble(restingHR);   //heart rate reserve 
double upperEndZone= (reserveHR*.85)+Double.parseDouble(restingHR); 
double lowerEndZone= (reserveHR*.50)+Double.parseDouble(restingHR); 

boolean isTargetRateLow= lowerEndZone<= Double.parseDouble(restingHR) ; 

有沒有辦法將兩個操作符放在一個布爾語句中?我認爲這會解決我的問題。

回答

3

通過在一個布爾聲明兩家運營商你的意思是:

布爾布爾=一個< = B & & C> = d

如AND運算符(& &)

+0

準確地說,謝謝! – PedroPovedaQ 2010-07-18 14:01:15

3

這是你所需要的?

boolean isInbetween = (x >= lowerEndZone) && (x <= upperEndZone); 
+0

是的,謝謝! – PedroPovedaQ 2010-07-18 14:00:40

2

你的意思是類似於

// determine if x is in [a,b] 
bool in_fully_closed_interval = (a <= x) && (x <= b); 

// determine if x is in [a,b) 
bool in_closed_left_open_right_interval = (a <= x) && (x < b); 

// determine if x is in (a,b] 
bool in_open_left_closed_right_interval = (a < x) && (x <= b); 

// determine if x is in (a,b) 
bool in_open_interval = (a < x) && (x < b); 

是的,我知道其他人已發佈此。我這樣做是爲了展示我認爲更可讀的變體。

+0

+1;關於你最後的陳述:你不需要對回答問題保持如此謹慎的態度。我們一直歡迎善意的貢獻。 – polygenelubricants 2010-07-18 15:29:03

相關問題