I am new to java and I really enjoy this new learning experience. I got assigned a task where we have to create a simple calendar where the user needs to put a date and the program needs to tell you if it is a correct date.
However, I receive an error code that I'm using a bad operand type. I cant use an int with a boolean type. However, I do not seem to be able to find the problem.
All help and insight is very welcome
public static void main(String[] args) {
Scanner userInput = new Scanner(System.in); // Creating the new Scanner
System.out.print("Choose a day: "); //Asking for user to introduce a day
int day = userInput.nextInt();
System.out.print("Choose a month: "); // Asking user to introduce a month
int month = userInput.nextInt();
System.out.print("Choose a year: "); //Asking user to introduce a year
int year = userInput.nextInt();
if ( (1<= day <= 31) && (1 <= month <= 12) && (year >= 0)){ //marking the limits of day, month and year
System.out.println(" Congratulations, the date you introduced : " + day + month + year +"exists!!");
} else if ( (day > 30) && (month = 2 || 4 || 6 || 9 || 10) && (year >= 0)){ //marking the months which have 30 days
System.out.println(" Oh no, the date you introduced : " + day + month + year +" does Not exists!! You can always try again");
}else if ( (day > 28) && (month = 2) && (year >=0)){ //marking month February
System.out.println(" Oh no, the date you introduced : " + day + month + year +" does Not exists!! You can always try again");
} else {
System.out.println(" Oh no, the date you introduced : " + day + month + year +" does Not exists!! You can always try again");
}
}
You must use if ( (day >= 1 && day <= 31) && (month >= 1 && month <= 12) && (year >= 0))
Also, for every month you need to put month == 2 || month == 4...
month == 2 || month == 4...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.