简体   繁体   中英

Java Prompt user to input birthdate

I am trying to come up with a code to prompt the user to enter their birthdate. I am completely unsure how to do this as this is my first programming class. //Get the date of birth is where I need to enter this code. If someone could walk me through it that would be great, thank you!

 // 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) { } } 

There are a number of ways to do this. One such way will be to use Scanner .

For example, this code allows a user to read a number from System.in. The "standard" input stream. This stream is already open and ready to supply input data. Typically this stream corresponds to keyboard input or another input source specified by the host environment or user.

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);
    }
}

To calculate age, you need curMonth, curDay, curYear, birthMonth, birtyDay, birthYear. and cur...s are set in start of Main function.

And, new Scanner have been created in few lines above. So read a line and split into variables (integer).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM