java - 我的逻辑运算符 if 语句中的无尽错误
问题描述
我对 java 很陌生,因此我不确定如何修复我的代码上的错误,在 java 上尝试了一个逻辑运算符,但被每个语句行上的重复错误所淹没(“非法开始类型”,“非法开始of expression”、“not a statement”和“ ';' 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";
}
解决方案
问题
- 比较运算符通常是二元运算符。所以他们需要两个参数。
- Java 期望返回比较
boolean
,这是通过&&
or实现的||
。问题中使用的某些运算符按位计算,仅当操作数为&
时才会返回。如果错误地使用最终导致,它将返回意想不到的结果boolean
boolean
boolean
- 字符串应与“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";
}
}
推荐阅读
- c++ - 在 Visual Studio 中,是否有像 C# 一样的 C++ 代码注释?
- angular - Angular-Material:本机日期时间选择器组件,无秒
- hibernate - Hibernate Search 使用其他表中的值提升
- sql - 考虑到给定的间隔,我如何加入两个表
- python - AWS Lambda:使用moderngl / glcontext时找不到libGL.so包
- bash - 在 Bash 脚本中循环时附加到文件,但每次执行脚本时都会打开一个新文件
- ruby - 如何使用 Ruby 检查 DNS 记录是否存在?
- excel - 为什么我在工作表上找不到评论?
- c# - 如何将firestore时间戳转换为不同的格式en c#
- python - 如何使用 discord.py 在 Discord 机器人中正确实现随机整数命令?