每日一练126——Java我在2099年多大呢?(8kyu)

题目

菲利普刚刚四岁,他想知道未来几年他将会有多大年纪,如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!";
  }
}

后记

我这丑代码还是可以改改的。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 爱情,是两个人之间一场旷日持久而又不遗余力的勾心斗角。 1 两个月前的某个晚上。“给我一杯尼格龙尼用手工冰块加两块...
    迟二白阅读 1,368评论 15 13
  • 你的形象与你的个人努力息息相关 你是长得美还是长得丑 其实也有相当一部分由你自己来决定 很多明星也如此 有的年轻帅...
    妍值百科阅读 1,440评论 0 1
  • 好像,我喜欢的人都不喜欢我哎。 时常怀疑自己,哪里都怀疑,我是不是性格不好,我是不是长得不好,我是不是太矮了,我是...
    南浔_南浔_难寻阅读 496评论 0 0
  • 之前定的计划,今天终于把之前落下的作业写完了,只是完成了书面任务,背诵没有完成,英语但是读熟了。 今天是小年,过了...
    大名170房静静阅读 157评论 0 0
  • 1.什么是CAS CAS是一种无锁执行的算法原理。全称Compare And Swap其算法核心思想如下 2.Un...
    胖小白_d797阅读 405评论 0 0