逻辑操作符if语句中出现无尽错误



我对java很陌生,因此我不确定如何修复我代码上的错误,尝试在Java上使用逻辑运算符,但被每条语句行的重复错误弄得不知所措("非法的类型开始", "非法的表达式开始", "不是一个语句"one_answers";', ' expected")我是否使用了错误的代码形式?

if (gender = "m") {
if (age >=18 & <30 && vo2max >= 40 && <=60){
EligibleSport = "Basketball";
}
if (age >= 18 & <26 && vo2max >= 62 & <=74){
EligibleSport = "Biycling";
}
if (age >= 18 & <26 && vo2max >= 55 & <=67){
EligibleSport = "Canoeing";
}
if (age >= 18 & <22 && vo2max >= 52 & <=58){
EligibleSport = "Gymnastics";
}
if (age >= 10 & <25 && vo2max >= 50 & <=70){
EligibleSport = "Swimming";
}

问题

  1. 比较操作符一般为二进制操作符。所以他们需要两个参数。
  2. Java期望比较返回boolean,通过&&||实现。问题中使用的一些操作符为位&,只有当操作数为boolean时,它们才会返回boolean。如果不正确地使用boolean,它将返回意想不到的结果
  3. 字符串应该与"equals"或"equalsIgnoreCase">

解决方案
if ("m".equals(gender)) {
if (age >= 18 && age < 30 && vo2max >= 40 && vo2max <= 60) {
eligibleSport = "Basketball";
}
if (age >= 18 && age < 26 && vo2max >= 62 && vo2max <= 74) {
eligibleSport = "Biycling";
}
if (age >= 18 && age < 26 && vo2max >= 55 && vo2max <= 67) {
eligibleSport = "Canoeing";
}
if (age >= 18 && age < 22 && vo2max >= 52 && vo2max <= 58) {
eligibleSport = "Gymnastics";
}
if (age >= 10 && age < 25 && vo2max >= 50 && vo2max <= 70) {
eligibleSport = "Swimming";
}
}

最新更新