我在 Java 中作業。所以我想做的是,從subscriberPackage 獲取用戶輸入
public static String subscriberPackage() { //Method to get the customers name
Scanner sc = new Scanner(System.in);
System.out.println("\nPlease choose a package the customer would like:");
System.out.println("Enter Bronze, Silver or Gold to select a package.");
String subscriptionPackage = sc.nextLine(); //Get subscription package from the user
while (!(subscriptionPackage.equalsIgnoreCase("Bronze") || subscriptionPackage.equalsIgnoreCase("Silver") || subscriptionPackage.equalsIgnoreCase("Gold"))) {
System.err.println("This is not a valid package!");
System.err.println("Please try again!");
System.out.println("Please choose a package the customer would like:");
System.out.println("Bronze, Silver or Gold.");
subscriptionPackage = sc.nextLine();
//If the package entered is not Bronze, Silver or Gold, the user is asked to re-enter till the while condition is met
}
return subscriptionPackage; //Returns the subscription package to the newSubscription method
}
和訂閱者持續時間
public static int subscriberDuration() { //Method to get the subscription duration
Scanner sc = new Scanner(System.in);
System.out.println("\nPlease choose the subscription duration");
System.out.println("It must be either 1, 3, 6 or 12 months.");
System.out.println("Please now enter the subscription duration the customer would like");
int subscriptionDuration = sc.nextInt(); //Get the subscription duration from the user
while (subscriptionDuration != 1 && subscriptionDuration != 3 && subscriptionDuration != 6 && subscriptionDuration != 12) {
System.err.println("This is not a valid duration, please try again!");
System.err.println("It must be either 1, 3, 6 or 12 months.");
subscriptionDuration = sc.nextInt();
//If the duration entered is not 1, 3, 6 or 12, the user is asked to re-enter till the while condition is met
}
return subscriptionDuration; //Returns the subscription duration to the newSubscription method
}
并在subscriberBill 中計算成本。
private static int subscriberBill(String subscriptionPackage, int subscriptionDuration) {
int subscriptionCost = 0;
int[] subsBronzePackageCosts = new int[1000];
subsBronzePackageCosts[0] = 600;
subsBronzePackageCosts[1] = 500;
subsBronzePackageCosts[2] = 400;
subsBronzePackageCosts[3] = 300;
int count = 0;
for(int i = 1; i <= 12; i =3){
if (subscriptionPackage.equals(subscriptionPackage) && (subscriptionDuration == subscriptionDuration)) {
subscriptionCost = (subsBronzePackageCosts[i] i count);
count ;
System.out.println("\nThe cost of this subscription is: " subscriptionCost);
return subscriptionCost;
}
}
return subscriptionCost;
}
}
我已經設法通過這些資訊并開始添加青銅級訂閱包的成本。我已經使用陣列完成了這項作業:
int[] subsBronzePackageCosts = new int[1000];
subsBronzePackageCosts[0] = 600;
subsBronzePackageCosts[1] = 500;
subsBronzePackageCosts[2] = 400;
subsBronzePackageCosts[3] = 300;
我正在嘗試遍歷所述陣列,例如,用戶將持續時間設定為 3,它將使用 for 回圈遍歷我的陣列并設定subscriptionCost為適當的值。在本例中,為 400。
The part I'm stuck with, is iterating through the array with the for loop. I have it so it's only adding 1 and reporting 500 every time. I did have a solution working for if else statements for each different package type and duration, but obviously, this isn't ideal practice and causes repetitive code, which I'm trying to avoid.
Thanks in advance!
EDIT: To add a bit more context. I have three tiers of subscriptionPackage to work with: Bronze, Silver and Gold. All of these packages have different pricings and the pricings change with the length of subscriptionDuration that the user enters.
The packages/pricings are: Bronze prices are 600 for 1 month, 500 for 3 months, 400 for 6 months and 300 for 12 months.
Silver prices are 800 for 1 month, 700 for 3 months, 600 for 6 months and 500 for 12 months.
Gold prices are 999 for 1 month, 899 for 3 months, 799 for 6 months and 699 for 12 months.
uj5u.com熱心網友回復:
你這里的代碼看起來有點亂。但我想我會指出你的 if 陳述句中的邏輯......
subscriptionPackage.equals(subscriptionPackage) ...這總是正確的,對吧?所以你可以洗掉它。
subscriptionDuraction == suscriptionDuration ...這也總是正確的,對嗎?所以你的整個 if 陳述句消失了。
所以這個 if 陳述句總是計算為真,這意味著你總是會在 if 塊中點擊 return 陳述句,i = 1,在你的陣列中,[1] = 500。
uj5u.com熱心網友回復:
我建議嘗試使用類,例如:
// Going this far could be up for debate
public enum SubscriptionType { GOLD, SILVER, BRONZE }
public enum SubscriptionDuration { MONTHLY, QUARTERLY, BI_ANNUALLY, ANNUALLY }
public class SubscriptionFee {
SubscriptionType type; // Replace with String
SubscriptionDuration duration; // Replace with Integer
int cost;
}
如果您更愿意分別使用和type,則兩者都有待討論。durationStringInteger
您可以嘗試在當前代碼中實作這些,看看是否更容易回圈比較所有選項:
- 訂閱費型別
- 訂閱費.duration
到您從“用戶”那里收集的資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/418746.html
標籤:
