Java提示用户输入生日

时间:2016-04-14 02:31:34

标签: java

我正在尝试提供一个代码来提示用户输入他们的生日。我完全不确定如何做到这一点,因为这是我的第一个编程课程。 //获取出生日期是我需要输入此代码的地方。 如果有人能指引我,那就太棒了,谢谢!

//  Rental rates assignment
//  Pre-set main for testing (see DEBUG constant)

//  Required methods to be added:  calcAge(...), calcRateClass(...) and displayResult(...)
//  Also, insert code into main as indicated.

import java.util.*;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class RentalRates {
  private static final boolean DEBUG = true;

  private static final String BEST_RATE = "Best rate - $40.00 per day or $200.00 per week.";
  private static final String RISK_RATE_1 = "Risk rate 1-$50.00 per day or $255.00 per week.";
  private static final String RISK_RATE_2 = "Risk rate 2-$57.00 per day or $285.00 per week.";
  private static final String RISK_RATE_3 = "Risk rate 3-$%4.2f per day or $%5.2f per week.";

  public static void main(String[] args) {
    Calendar cal = Calendar.getInstance();
    int curMonth = cal.get(Calendar.MONTH) + 1;
    int curDay = cal.get(Calendar.DAY_OF_MONTH);
    int curYear = cal.get(Calendar.YEAR);
    int birthMonth = 0; //this means they are being set to a default value that you should not use
    int birthDay = 0; //this means they are being set to a default value that you should not use
    int birthYear = 0; //this means they are being set to a default value that you should not use
    String gender = "";
    int age = 0;
    String rateResult;

    // Testing mode...	
    if (DEBUG == false) {
      // Establish a 'current' date for testing...
      curMonth = 2;
      curDay = 1;
      curYear = 2016;

      System.out.println("First test case: Renter is not old enough to rent...");
      birthMonth = 2;
      birthDay = 2;
      birthYear = 1991;
      gender = "m";
      age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
      rateResult = calcRateClass(age, gender);
      displayResults(gender, age, rateResult);

      System.out.println("\nSecond test case: Renter is barely old enough (57/285)...");
      birthMonth = 2;
      birthDay = 1;
      birthYear = 1991;
      gender = "m";
      age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
      rateResult = calcRateClass(age, gender);
      displayResults(gender, age, rateResult);

      System.out.println("\nThird test case: Renter is 35 and male (40/200)...");
      birthMonth = 1;
      birthDay = 1;
      birthYear = 1981;
      gender = "m";
      age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
      rateResult = calcRateClass(age, gender);
      displayResults(gender, age, rateResult);

      System.out.println("\nFourth test case: Renter is 35 and female (40/200)...");
      birthMonth = 1;
      birthDay = 1;
      birthYear = 1981;
      gender = "f";
      age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
      rateResult = calcRateClass(age, gender);
      displayResults(gender, age, rateResult);

      System.out.println("\nFifth test case: Renter is 30 and male (57/285)...");
      birthMonth = 1;
      birthDay = 1;
      birthYear = 1986;
      gender = "m";
      age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
      rateResult = calcRateClass(age, gender);
      displayResults(gender, age, rateResult);

      System.out.println("\nSixth test case: Renter is 30 and female (40/200)...");
      birthMonth = 1;
      birthDay = 1;
      birthYear = 1986;
      gender = "f";
      age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
      rateResult = calcRateClass(age, gender);
      displayResults(gender, age, rateResult);

      System.out.println("\nSeventh test case: Renter is 76 and male (62/255)...");
      birthMonth = 1;
      birthDay = 1;
      birthYear = 1940;
      gender = "m";
      age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
      rateResult = calcRateClass(age, gender);
      displayResults(gender, age, rateResult);

      System.out.println("\nEighth test case: Renter is 76 and female (68/270)...");
      birthMonth = 1;
      birthDay = 1;
      birthYear = 1940;
      gender = "f";
      age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);
      rateResult = calcRateClass(age, gender);
      displayResults(gender, age, rateResult);
    } else {
      Scanner kb = new Scanner(System.in);
      System.out.println("Welcome to the car renter's rate finder.");


      // If you are attempting the EC, use the Calendar class to get today's date...
      //    Your code goes here...
      System.out.println("Today's date is: " + curMonth + "/" + curDay + "/" + curYear);



      // Get the gender...
      //    Your code goes here...
      Scanner reader = new Scanner(System.in);
      System.out.println("Please enter the renter's gender (m/f): ");
      gender = reader.nextLine();

      // Get the date of birth...
      //    Your code goes here...
      System.out.println("Please enter the renters date of birth (mm dd yyyy): ");




      // Get age...
      age = calcAge(curMonth, curDay, curYear, birthMonth, birthDay, birthYear);

      // Get the rental rate...
      rateResult = calcRateClass(age, gender);

      // Display the results...
      displayResults(gender, age, rateResult);
    }
  }

  public static int calcAge(int curMonth, int curDay, int curYear, int birthMonth, int birthDay, int birthYear) {
    int age = (curYear - birthYear);
    if (curMonth > birthMonth) {
      age += 1;
    } else if (curMonth == birthMonth) {
      if (curDay > birthDay) {
        age += 1;
      }
    }
    return age;
  }

  public static String calcRateClass(int age, String gender) {
    if ((age >= 33 && age <= 65 && gender == Character.toString('m')) || (age >= 30 && age <= 62 && gender == Character.toString('f'))) {
      return BEST_RATE;
    } else if (age >= 25 && age <= 29 && gender == Character.toString('f')) {
      return RISK_RATE_1;
    }

    if (age >= 25 && age <= 32 && gender == Character.toString('m')) {
      return RISK_RATE_2;
    }
    if (age >= 66 && gender == Character.toString('m') || age >= 63 && gender == Character.toString('f')) {
      return RISK_RATE_3;

    }
    return BEST_RATE;
  }

  public static void displayResults(String gender, int age, String rateResult) {

  }
}

2 个答案:

答案 0 :(得分:0)

有很多方法可以做到这一点。一种方法是使用Scanner

例如,此代码允许用户从System.in读取数字。 “标准”输入流。此流已打开并准备好提供输入数据。通常,此流对应于键盘输入或主机环境或用户指定的其他输入源。

import java.util.Scanner;

public class TestUserInput {

    public static void main(String[] args) {
       System.out.println("Enter what you want to key in:");
       String uInput;

       Scanner scan = new Scanner(System.in);
       uInput = scan.nextLine();

       scan.close();            
       System.out.println(uInput);
    }
}

答案 1 :(得分:0)

要计算年龄,你需要curMonth,curDay,curYear,birthMonth,birtyDay,birthYear。和cur ... s在Main函数的开头设置。

并且,上面几行创建了新的Scanner。所以读一行并分成变量(整数)。

String date = reader.nextLine();
birthMonth = Integer.parseInt(date.split(" ")[0]);
birthDay = Integer.parseInt(date.split(" ")[1]);
birthYear = Integer.parseInt(date.split(" ")[2]);