瀏覽代碼

(feat) Implement Lab 5 exercise 7 - show user age

main
Rob Hallam 1 年之前
父節點
當前提交
263a2b39a3
共有 1 個文件被更改,包括 33 次插入2 次删除
  1. +33
    -2
      week1.java

+ 33
- 2
week1.java 查看文件

@@ -5,8 +5,8 @@ import java.util.concurrent.ThreadLocalRandom;

class runLabs {
public static void main(String[] args) {
new Lab4();
// new Lab5();
// new Lab4();
new Lab5();
// new Lab6();
// new Lab7();
}
@@ -368,6 +368,37 @@ class Lab5 extends Lab {
class five7 extends Exercise {
public five7() {
super(7);
showUserAge();
}

public void showUserAge() {
Scanner userInput = new Scanner(System.in);
System.out.println(String.format("Please input your DOB"));
System.out.println(String.format("input format is YYYY M D"));
String inputString = userInput.nextLine();

Scanner processInput = new Scanner(inputString);

int dobYear = processInput.nextInt();
int dobMonth = processInput.nextInt();
int dobDay = processInput.nextInt();
// input sanitisation could be nice...

Calendar userDOB = Calendar.getInstance();
// recall months are zero-indexed!
userDOB.set(dobYear, dobMonth-1, dobDay);

Calendar today = Calendar.getInstance();

System.out.println(String.format("Today:\t%s", today.getTime()));
System.out.println(String.format(
"DOB :\t%s (difference: %d years)",
userDOB.getTime(),
Lab4.yearsOld(today, userDOB)));

userInput.close();
processInput.close();

}
}
}


Loading…
取消
儲存