题目
菲利普刚刚四岁,他想知道未来几年他将会有多大年纪,如2090年或3044年。他的父母无法跟上计算,所以他们恳求你通过编写一个程序来帮助他们。可以回答菲利普无休止的问题。
你的任务是编写一个带有两个参数的函数:出生年份和相对于年份的年份。由于菲利普每天都变得更加好奇,他可能很快就会想知道他出生多少年,所以你的职能需要在未来和过去的两个日期工作。
以这种格式提供输出:对于未来的日期:“你是......年龄。” 对于过去的日期:“你将出生在...年。” 如果出生年份等于要求回报的年份:“你这一年就出生了!”
“......”将由数字代替,然后由单个空格继续。请注意,您需要考虑“年”和“年”,具体取决于结果。
祝好运!
测试用例:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;
public class SolutionTest {
AgeDiff age = new AgeDiff();
public static int born;
public static int year;
private static int pom;
@Test
public void testAge() {
assertEquals("You are 4 years old.", age.CalculateAge(2012, 2016));
assertEquals("You are 27 years old.", age.CalculateAge(1989, 2016));
assertEquals("You are 90 years old.", age.CalculateAge(2000, 2090));
assertEquals("You will be born in 10 years.", age.CalculateAge(2000, 1990));
assertEquals("You were born this very year!", age.CalculateAge(3400, 3400));
assertEquals("You are 2000 years old.", age.CalculateAge(900, 2900));
assertEquals("You will be born in 110 years.", age.CalculateAge(2010, 1900));
assertEquals("You will be born in 510 years.", age.CalculateAge(2010, 1500));
assertEquals("You are 1 year old.", age.CalculateAge(2011, 2012));
assertEquals("You will be born in 1 year.", age.CalculateAge(2000, 1999));
}
@Test
public void testRandomAge() {
for(int i = 1; i <= 100; i++){
born = (int) (100+Math.random()*4000);
year = (int) (100+Math.random()*4000);
pom = year-born;
if (pom == 0) assertEquals("You were born this very year!", age.CalculateAge(born, year));
if (pom == 1) assertEquals("You are 1 year old.", age.CalculateAge(born, year));
if (pom == -1) assertEquals("You will be born in 1 year.", age.CalculateAge(born, year));
if(pom > 0) assertEquals("You are " + pom + " years old.", age.CalculateAge(born, year));
else assertEquals("You will be born in " + (-pom) + " years.", age.CalculateAge(born, year));
}
}
}
解题
My
public class AgeDiff
{
public static String CalculateAge(int birth, int yearTo)
{
if (birth < yearTo) {
if (yearTo-birth == 1) {
return "You are 1 year old.";
} else {
return String.format("You are %s years old.", yearTo-birth);
}
} else if (birth > yearTo) {
if (birth-yearTo == 1) {
return "You will be born in 1 year.";
} else {
return String.format("You will be born in %s years.", birth-yearTo);
}
} else if (birth == yearTo) {
return "You were born this very year!";
} else {
return "Error!";
}
}
}
Other
public class AgeDiff {
public static String CalculateAge(int birth, int year) {
final int age = year - birth;
return
age == 0 ? "You were born this very year!" :
age > 0 ? String.format("You are %d year%s old.", age, age == 1 ? "" : "s") :
String.format("You will be born in %d year%s.", -age, -age == 1 ? "" : "s");
}
}
class AgeDiff
{
public static String CalculateAge(int birth, int yearTo)
{
int a = yearTo - birth;
if (a == 0) return "You were born this very year!";
if (a == 1) return "You are 1 year old.";
if (a == -1) return "You will be born in 1 year.";
return a > 0 ? "You are " + a + " years old." : "You will be born in " + (-a) + " years.";
}
}
public class AgeDiff
{
public static String CalculateAge(int birth, int yearTo)
{
int age = Math.abs(yearTo - birth);
String year = " years";
if (Math.abs(yearTo - birth) == 1) {year = " year";}
if (yearTo < birth) {return "You will be born in " + age + year + ".";}
if (birth < yearTo) {return "You are " + age + year + " old.";}
return "You were born this very year!";
}
}
后记
我这丑代码还是可以改改的。