Java 嗨,我想让我的if语句等于一个以上的数字
我知道这是不正确的,我无法理解。我需要改变我的整个方法吗?这个网站还说这个问题已经被问过了,但是他们都没有帮到我Java 嗨,我想让我的if语句等于一个以上的数字,java,if-statement,Java,If Statement,我知道这是不正确的,我无法理解。我需要改变我的整个方法吗?这个网站还说这个问题已经被问过了,但是他们都没有帮到我 public Date(int cMonth, int cDate, int cYear, int cDayToDate, String cStrMonth, int dayYear){ if (cMonth = 01 && 12){ month = cMonth; if (cMonth = 01,03,
public Date(int cMonth, int cDate, int cYear, int cDayToDate, String cStrMonth, int dayYear){
if (cMonth = 01 && 12){
month = cMonth;
if (cMonth = 01,03,05,07,08,10,12){
if (cDate <= 31 ){
date = cDate;
}// end of if
}// end of if(cMonth) months with 31 days
else if(cMonth = 04, 06, 09, 11){
if (cDate <=30){
date = cDate;
}
}// end of cMonth month within 30 days
public Date(int cMonth,int cDate,int cYear,int cDayToDate,String cStrMonth,int dayYear){
如果(cMonth=01&&12){
月=厘米月;
如果(cMonth=01,03,05,07,08,10,12){
如果(cDate您可以使用List
来执行此操作
List<Integer> monthList = new ArrayList<Integer>();
monthList.add(4);
monthList.add(6);
monthList.add(9);
monthList.add(11);
if(monthList.contains(cMonth)){
//do your work
}
List monthList=new ArrayList();
月清单。添加(4);
增加(6)个月清单;
增加(9);
增加(11);
如果(月列表包含(厘米月)){
//做你的工作
}
开关语句可能是最好的选择
public Date(int cMonth, int cDate, int cYear, int cDayToDate, String cStrMonth, int dayYear) {
switch (cMonth) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (cDate > 31)
throw new IllegalArgumentException();
break;
case 4:
case 6:
case 9:
case 11:
if (cDate > 30)
throw new IllegalArgumentException();
break;
case 2:
int days = isLeapYear(cYear) ? 29 : 28;
if (cDate > days)
throw new IllegalArgumentException();
break;
default:
throw new IllegalArgumentException();
}
date = cDate;
}
一些更接近你要求的东西,但一些远,远没有效率的东西是
public Date(int cMonth, int cDate, int cYear, int cDayToDate, String cStrMonth, int dayYear) {
if (Arrays.asList(1, 3, 5, 7, 8, 10, 12).contains(cMonth)) {
if (cDate > 31)
throw new IllegalArgumentException();
} else if (Arrays.asList(4, 6, 9, 11).contains(cMonth)) {
if (cDate > 30)
throw new IllegalArgumentException();
} else if (cMonth == 2) {
int days = isLeapYear(cYear) ? 29 : 28;
if (cDate > days)
throw new IllegalArgumentException();
} else {
throw new IllegalArgumentException();
}
date = cDate;
}
顺便说一句,以0
开头的数字是八进制的,因此08
和09
无效
if (cMonth = 01 && 12)
要做到这一点,月份必须是1和12,我认为这是不可能的。我想你应该检查一下月份是否在1和12之间,所以应该是
if (cMonth >= 1 && cMonth <= 12)
无法工作,您应该尝试切换或简单地执行以下操作(大量键入):
有几个问题:
- 比较是用
==
完成的,而不是=
=
是赋值
- 以
0
s开头的数字,如01
和02
等是八进制。您可能需要1
、2
等是十进制的
- 您希望
&
知道要与12
进行比较的对象,但&
不是这样的。到&
的两个操作数都需要指定要比较的对象
&&
表示“和”。如果您想跟随分支,如果cMonth
是1或如果是12,则需要|
(“或”)
cMonth=04、06、09、11
还需要您明确说明您想对06
、09
和11
执行什么操作
因此,您的最外层如果需要:
public Date(int cMonth, int cDate, int cYear, int cDayToDate, String cStrMonth, int dayYear){
if (cMonth == 1 || cMonth == 12){
// ...
}
else if(cMonth == 4 || cMonth == 6 || cMonth == 9 || cMonth == 11) {
// ...
}
// ...
}
应用上面的注释列表,我将把分支的内部留给您
另外:您可以使用switch
语句,而不是所有这些cMonth==
比较
public Date(int cMonth, int cDate, int cYear, int cDayToDate, String cStrMonth, int dayYear){
switch (cMonth) {
case 1:
case 12:
// ...
break;
case 4:
case 6:
case 9:
case 11:
// ...
break;
}
// ...
}
Set set1=新的HashSet(新的整数[]{1,3,5,7,8,10,12});
Set set2=新的HashSet(新的整数[]{4,6,9,11});
如果(cMonth==1 | | cMonth==12){
如果(设置1.contains(cMonth)){
如果(cDate没有这样的快捷语法。你可以将所有的值放在列表中,并测试其中是否包含任何值。另外,一个=
是赋值。没有一个值同时是八进制1和十进制12。注意你的整数文本。08
,09
不会编译你想要一个集,而不是一个列表,如果你只想要的话要检查它是否包含内容。@ernest_k OP需要更新她的代码,因为她编写的代码类似于04
,即使类型匹配,当转换为字符串时,cMonth将是“4”
而不是“04”
。@lucumt添加了+1,数组。列表(…)
可能更干净,请参阅我的答案。
public Date(int cMonth, int cDate, int cYear, int cDayToDate, String cStrMonth, int dayYear){
if (cMonth == 1 || cMonth == 12){
// ...
}
else if(cMonth == 4 || cMonth == 6 || cMonth == 9 || cMonth == 11) {
// ...
}
// ...
}
public Date(int cMonth, int cDate, int cYear, int cDayToDate, String cStrMonth, int dayYear){
switch (cMonth) {
case 1:
case 12:
// ...
break;
case 4:
case 6:
case 9:
case 11:
// ...
break;
}
// ...
}
Set<Integer> set1 = new HashSet<Integer>(new Integer[]{1,3,5,7,8,10,12});
Set<Integer> set2 = new HashSet<Integer>(new Integer[]{4,6,9,11});
if(cMonth == 1 || cMonth == 12){
if(set1.contains(cMonth)){
if (cDate <= 31 ){
date = cDate;
}
}
else if(set2.contains(cMonth)){
if (cDate <= 30 ){
date = cDate;
}
}
}