私は、ユーザに誕生日を入力するよう促すコードを提示しようとしています。これは私の最初のプログラミングクラスなので、これをどうやって行うのか完全にはわかりません。 //生年月日を取得します。このコードを入力する必要があります。 誰かがそれを通って私を歩くことができれば、それは素晴らしいだろう、ありがとう!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... \t
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) {
}
}
よろしくお願いします! –