2016-04-14 14 views
0

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

 
    } 
 
}

答えて

0

これを行うにはいくつかの方法があります。そのような方法の1つは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); 
    } 
} 
0

年齢を計算するには、curMonth、curDay、curYear、birthMonth、birtyDay、birthYearが必要です。およびcur ...は、Main関数の先頭に設定されます。

さらに、新しいスキャナは、上記の数行で作成されています。したがって、行を読み、変数(整数)に分割します。

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

よろしくお願いします! –

関連する問題