+ 3
Can i take two if else statement inside an if else statement...
if no then what can i do at that circumstances???
9 ответов
+ 3
*UPDATE*
I cleaned up your code some and added the day to it so you can display full birth date. Hope this helps.
https://code.sololearn.com/czX2cthkY0Tt/#java
class Birth {
private int birthMonth = 0, birthDay = 0, birthYear = 0;
private int age = 0;
void CurrentAge(int birthMonth, int birthDay,int birthYear)
{
this.birthMonth = birthMonth;
this.birthDay = birthDay;
this.birthYear = birthYear;
if(this.birthMonth > 2 && this.birthMonth <= 12) {
age = 2017 - this.birthYear;
}
else if(this.birthMonth < 2 && this.birthMonth >= 1) {
age = 2018 - this.birthYear;
}
System.out.println("Age: " + age);
System.out.println("Born: " + this.birthMonth + "/" + this.birthDay + "/" + this.birthYear);
}
}
class Program {
public static void main(String args[])
{
Birth person1 = new Birth();
person1.CurrentAge(5,20,2002);
}
}
:::::::::: OUTPUT ::::::::::::
Age: 15
Born: 5/20/2002
+ 3
Yes...
if(condition){
if(anotherCondition){
//code
}else if(condition){
//more code.
} else{
//more...
}
}else if(condition){
//Another code... Be careful with logic.
} else{
// There are other ways to resume this.
}
+ 2
yu can
+ 2
You can nest as many IF statements as you feel like. When you have a question like that, you should just test it out and see what happens. You learn a lot from trial and error, as well as monitoring behaviors based upon changes you make.
However, what exactly are you trying to do? Just because you can endlessly nest IF statements doesn't mean it's the best means of resolving a particular issue. Give me more information and I can point you in the right direction.
+ 1
can u explain me by an example?
from this code u can explain me if i also want to add date!!!!
https://code.sololearn.com/c87q2pX4m98r/?ref=app
+ 1
If I understood you correctly, below is how you also add in the date of their birth. However, take note that you weren't receiving the data for the day, so it's month/year. Also note that the accept() function is kinda redundant. I would just add that code to the top of your current_age() function or go about it differently. Give me a moment and I'll throw another example together for you.
Let me know if you need further help or explanation.
https://code.sololearn.com/czX2cthkY0Tt/#java
class abc {
int birthYear, birthMonth, age;
void accept(int monthBorn, int yearBorn)
{
birthMonth = monthBorn;
birthYear = yearBorn;
}
void current_age()
{
if(birthMonth > 2 && birthMonth <= 12) {
age = 2017 - birthYear;
}
else if(birthMonth < 2 && birthMonth >= 1) {
age = 2018 - birthYear;
}
System.out.println("Age: " + age);
System.out.println("Born: " + birthMonth + "/" + birthYear);
}
public static void main(String args[])
{
abc obj = new abc();
obj.accept(5,2002);
obj.current_age();
}
}
:::::::::::::::::::::OUTPUT::::::::::::::::::::::::::::::::::
Age: 15
Born: 5/2002
+ 1
thnxx jakob
+ 1
You're welcome, Harsh. I hope that helps.
0
Yes, two if else statements can be nested together and the code will compile and run smoothly