I wrote this code and it somehow works incorrectly.

if (years == 0 ){ age = ""; } if (years == 1 ){ age = "год"; } if (years <=4){ age = "года"; } if (years <=20){ age = "лет"; } if (years ==21){ age = "год"; } if (years <=24){ age = "года"; } if (years <=30){ age = "лет"; } if (years ==31){ age = "год"; } if (years <=34){ age = "года"; } if (years <=40){ age = "лет"; } if (years ==41){ age = "год"; } if (years <=44){ age = "года"; } if (years <=50){ age = "лет"; } 

At any age writes "years"

Closed due to the fact that off-topic participants Kromster , user192664, 0xdb , Jarvis_J , mymedia 9 Nov '18 at 5:56 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, Community Spirit, Jarvis_J
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    So you have the last condition years <= 50 for all ages)) Here is the variable and gets the value "years". - Igor Gor
  • I also thought about it, and how to fix it, I tried it with else but it didn't work out - Fussion Bart
  • Not in the manual to write every unit? - Fussion Bart
  • How about making all the teams strictly the other way around? - Fussion Bart
  • four
    You do not feel that the word depends on the last digit? As a last resort, from the last two? - Igor

3 answers 3

Something strange begins at the age of 51. We'll have to add more code to the function (

 function ageToText(years) { if (years == 0) { age = ""; } else if (years == 1) { age = "год"; } else if (years <= 4) { age = "года"; } else if (years <= 20) { age = "лет"; } else if (years == 21) { age = "год"; } else if (years <= 24) { age = "года"; } else if (years <= 30) { age = "лет"; } else if (years == 31) { age = "год"; } else if (years <= 34) { age = "года"; } else if (years <= 40) { age = "лет"; } else if (years == 41) { age = "год"; } else if (years <= 44) { age = "года"; } else if (years <= 50) { age = "лет"; } return years + " " + age; } for (var i = 0; i < 110; i++) { $("#ages").append(ageToText(i) + "<br/>"); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="ages"></div> 

And here is how it should be:

 function ageToText(years) { if (years == 0) return ""; var age = "лет"; var last1 = years % 10; var last2 = years % 100; if (last2 >= 5 && last2 <= 20) { //age = "лет"; } else if (last1 == 1) { age = "год"; } else if (last1 >= 2 && last1 <= 4) { age = "года"; } return years + " " + age; } for (var i = 0; i < 130; i++) { $("#ages").append(ageToText(i) + "<br/>"); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="ages"></div> 

    My vision of this code:

     int lastNumberOfYear = years; lastNumberOfYear %= 10; if (lastNumberOfYear == 0 || lastNumberOfYear == 5 || lastNumberOfYear == 6 || lastNumberOfYear == 7 || lastNumberOfYear == 8 || lastNumberOfYear == 9) { age = "лет"; } if (lastNumberOfYear == 1) { age = "год"; } if (lastNumberOfYear == 2 || lastNumberOfYear == 3 || lastNumberOfYear == 4) { age = "года"; } System.out.println(age + " " + years); 
    • Your vision does not see teenagers and some long-livers. - Igor
    • Really. At 11, 12, 13, 14, is incorrect. It is necessary to take into account the first digit. - KonstantinLugowoy
    • Long-livers with the same numbers: 11, 12, 13, 14, .... 111, 112, 113, 114, .... - KonstantinLugowoy

    For me, your codes are just puzzled, I did it in the most primitive way, but unfortunately this is not a cycle, but simply an age from 0 to 100. And I realized that optimization is not my fad :-)

     String error = "Ошибка возраста"; if (years <=100){ age = "лет"; } if (years <=94){ age = "года"; } if (years <=91){ age = "год"; } if (years <=90){ age = "лет"; } if (years <=84){ age = "года"; } if (years ==81){ age = "год"; } if (years <=80){ age = "лет"; } if (years <=74){ age = "года"; } if (years ==71){ age = "год"; } if (years <=70){ age = "лет"; } if (years <=64){ age = "года"; } if (years ==61){ age = "год"; } if (years <=60){ age = "лет"; } if (years <=54){ age = "года"; } if (years ==51){ age = "год"; } if (years <=60){ age = "лет"; } if (years <=54){ age = "года"; } if (years <=50){ age = "лет"; } if (years <=44){ age = "года"; } if (years ==41){ age = "год"; } if (years <=40){ age = "лет"; } if (years <=34){ age = "года"; } if (years ==31){ age = "год"; } if (years <=30){ age = "лет"; } if (years <=24){ age = "года"; } if (years ==21){ age = "год"; } if (years <=20){ age = "лет"; } if (years <=4){ age = "года"; } if (years == 1 ){ age = "год"; } if (years == 0 ){ age = error; } 
    • one
      those. "101 year" or "102 year" is not destiny? They also gave you the correct recommendation to "calculate" by the last digit. And if in the future you need to do the amount in words do the same? - Igor Kudryashov
    • I'm still too stupid for your codes, so I understand, but what does the percentage in the code? - Fussion Bart
    • @FussionBart The % operator ( a % b ) calculates the remainder of dividing a by b . - Igor