我正在為我所在的班級做一個項目,任務是制作一個需要Netbeans3 個輸入的程序,人的身高,背部問題心臟問題。老師說這兩題要用boolean。他希望我們用它來inputBack.equals("N")看看它是否等于N我得到的輸入,無論如何,如果有人可以幫助我,我會將我的代碼放在下面,那就太好了!基本上,程序僅在我更改高度時輸出不同,但我需要它在b或h時顯示其他內容Y。double H;String b, h;b = back.getText();h = heart.getText();H = Double.parseDouble(height.getText());if (h.equals("Y") || b.equals("Y")) { output.setText("Sorry, its not safe for you to ride the coaster");}if ((H >= 122 && H <= 188) && (h.equals("N") || b.equals("N"))) { output.setText("You are cleared to ride, have fun!");} else if (b.equals("Y") || h.equals("Y")) { output.setText("Sorry, its not safe for you to ride the coaster");} else { output.setText("Sorry, its not safe for you to ride the coaster");}
3 回答

MMTTMM
TA貢獻1869條經驗 獲得超4個贊
你的錯誤在這一行:
if ((H >= 122 && H <= 188) && (h.equals("N") || b.equals("N"))) {
事實上,如果你的身高合適,即使你有另外兩個問題之一,你也可以去坐過山車。如果您輸入 h="N" 和 b="Y",則條件h.equals("N") || b.equals("N")
將為 true,因為 h="N"。最好的做法是將這一行替換為:
if ((H >= 122 && H <= 188) && (h.equals("N") && b.equals("N"))) {
你也可以簡化你的代碼,你放了太多的if...

30秒到達戰場
TA貢獻1828條經驗 獲得超6個贊
您有太多多余的 if/else 語句。您可以像這樣簡化您的代碼:
//Heart or back problem, so no riding
if (h.equals("Y") || b.equals("Y")) {
output.setText("Sorry, its not safe for you to ride the coaster");
}
else { //health ok, check height
if (H >= 122 && H <= 188)
output.setText("You are cleared to ride, have fun!");
else
output.setText("You are outside the height requirements, you can't ride")
}
另請記住,如果用戶輸入的高度值不是數字,您將拋出異常。
添加回答
舉報
0/150
提交
取消