我的if语句存在一些问题。错误显示此信息 Problem 你能帮我弄清楚它有什么问题吗?主要任务是汽车保险公司根据申请人的年龄和性别分为4类保险。
这是代码:
String gender, age;
char group;
int genderint, ageint;
gender = JOptionPane.showInputDialog("Please specify your gender(1 for male, 0 for female)");
age = JOptionPane.showInputDialog("Please enter your age");
genderint = Integer.parseInt(gender);
ageint = Integer.parseInt(age);
if (gender = 0 || 1 && age = > 18 && < 26) {
group = "Category A";
} else if (gender = 0 && age = > 27 && < 60) {
group = "Category B";
} else if (gender = 1 && age = > 27 && < 60) {
group = "Category C";
} else if (gender = 0 || 1 && age = > 60) {
group = "Category D";
} else if (gender = 0 || 1 && age = < 18) {
JOptionPane.showMessageDialog(null, "Sorry, you're too young");
}
JOptionPane.showMessageDialog(null, "You have been assignet to" + group);
}
}
答案 0 :(得分:2)
试试这个,
==
进行比较<=
和>=
or
组条件<=
而非=<
,使用>=
代替=>
使用genderint
和ageint
变量进行int比较
if ((genderint == 0 || genderint ==1) && (ageint >=18 && ageint < 26)) {
group = "Category A";
} else if(genderint == 0 && ageint >= 27 && ageint < 60){
group = "Category B";
} else if(genderint == 1 && ageint >= 27 && ageint < 60) {
group = "Category C";
} else if((genderint == 0 || genderint == 1) && ageint >= 60) {
group = "Category D";
} else if((genderint == 0 || genderint ==1) && ageint <18){
JOptionPane.showMessageDialog(null,"Sorry, you're too young");
}
答案 1 :(得分:1)
首先要做的事情......
= >
替换为>=
,将=<
替换为<=
,将=
替换为==
(当比较性别时)if条件。string
,java字符串按.equals()
进行比较,而不是&#39; ==&#39;。答案 2 :(得分:1)
你有几个问题:
= <
运算符写为<=
(无空格,=
之后<
};同样适用于= >
(即>=
)gender = 0 || 1
需要genderint == 0 || genderint == 1
age = >27 && <60
应为age >= 27 && age < 60
答案 3 :(得分:1)
首先,您应该在条件中使用genderint
和ageint
。
其次,您似乎正在发明一些不存在的运算符:
gender = 0 || 1
应为(genderint == 0 || genderint == 1)
- 比较需要==
age = >18 && <26
应为ageint >= 18 && ageint < 26
- 大于或等于>=
答案 4 :(得分:0)
class