我有一個關于任務的問題:我需要實作一個公共靜態方法getTotalAmount(),它以字串陣列和貨幣名稱的形式輸入帶有貨幣的錢包。該方法必須回傳指定貨幣的金額
方法引數:
一個字串陣列,包含不同貨幣、不同面額的鈔票和貨幣名稱(3 個字符的字串)。
我需要使用“break”和“continue”等控制指令來實作此方法。
程式應該如何作業:
String[] money1 = {"eur 10", "usd 1", "usd 10", "rub 50", "usd 5"};
App.getTotalAmount(money1, "usd"); // 16
String[] money2 = {"eur 10", "usd 1", "eur 5", "rub 100", "eur 20", "eur 100", "rub 200"};
App.getTotalAmount(money2, "eur"); // 135
String[] money3 = {"eur 10", "rub 50", "eur 5", "rub 10", "rub 10", "eur 100", "rub 200"};
App.getTotalAmount(money3, "rub"); // 270
任務通知:
1.要將字串轉換為整數,請使用Integer類的parseInt()方法。
2. 要從字串中提取子字串,請使用substring()方法。
var text = "some text";
text.substring(1, 6); // "ome t"
text.substring(7); // "xt"
我的代碼如下所示:
package com.arrays.problem6;
public class Currency {
public static void main(String[] args){
}
public static String getTotalAmount(String[] money,String currency){
int result=0;
for(String item: money){
if(currency==item.substring(0,3)){
// condition: if the specified rate is equal to the array element that is sliced ??from 0 - 3 index, then
// add to the counter what is left until the end of the element, converting the string into a number through parseInt()
}
result=Integer.parseInt(item.substring(4, item.length()));
}
return result;
}
}
我對 getTotalAmount() 方法的邏輯實作完全感到困惑。請幫我實作它。
uj5u.com熱心網友回復:
首先,永遠不要String
使用雙等號進行比較==
。而是使用a.equals(b)
.
其次,您可以使用a.contains(b)
檢查一個字串是否包含另一個字串。
然后,您可以使用貨幣長度 1 作為子字串的開始來獲取金額:
package com.arrays.problem6;
public class Currency {
public static void main(String[] args){
}
public static int getTotalAmount(String[] money,String currency){
int result=0;
for(String item: money){
if(item.contains(currency)){ // condition
result = Integer.parseInt(item.substring(currency.length() 1)); // add to the counter
}
}
return result;
}
}
如果您被迫使用比較來解決練習,您也可以使用a.equals(b)
代替a.contains(b)
:
package com.arrays.problem6;
public class Currency {
public static void main(String[] args){
}
public static int getTotalAmount(String[] money,String currency){
int result=0;
for(String item: money){
if(currency.equals(item.substring(0, currency.length()))){ // condition
result = Integer.parseInt(item.substring(currency.length() 1)); // add to the counter
}
}
return result;
}
}
編輯請注意,我將靜態方法的簽名從更改為String
,int
因為您正在計算數字并且可能想要回傳數字,而不是字串。
例如,如果您必須使用continue
并且break
您可以否定條件,那么您將檢查該專案是否不包含貨幣。然后你會使用continue
跳到下一個專案。
uj5u.com熱心網友回復:
這是一個更完整的例子。
public class Wallet {
enum Currency {
USD, EUR, RUB;
public String getName() {
return this.name().toLowerCase();
}
}
private static final String[] WALLET_1 = {"eur 10", "usd 1", "usd 10", "rub 50", "usd 5"};
private static final String[] WALLET_2 = {"eur 10", "usd 1", "eur 5", "rub 100", "eur 20", "eur 100", "rub 200"};
private static final String[] WALLET_3 = {"eur 10", "rub 50", "eur 5", "rub 10", "rub 10", "eur 100", "rub 200"};
public static void main(String... args) {
var amounts = new Integer[] {
getTotalAmount(WALLET_1, Currency.USD),
getTotalAmount(WALLET_2, Currency.EUR),
getTotalAmount(WALLET_3, Currency.RUB)
};
for(var i = 0; i < amounts.length; i ){
System.out.println("Wallet " (i 1) " amount is: " amounts[i]);
}
}
private static Integer getTotalAmount(String[] wallet, Currency currency) {
int amount = 0;
for (var note : wallet) {
var noteDetails = note.split(" ");
if (!currency.getName().equals(noteDetails[0])) {
continue;
}
amount = amount Integer.parseInt(noteDetails[1]);
}
return amount;
}
}
因此,讓我們分解更改:
- 我沒有使用字串進行貨幣比較,而是使用
Enum
模擬貨幣——在我們的例子中是歐元、美元和盧布。 - 而不是使用幻數來假設我正在使用的開始和結束索引的子字串,
split
它將回傳一個包含兩個字串的陣列,一個用于貨幣,一個用于字串形式的金額。請注意,我確實理解這是您的任務的要求,因此您可能需要更改它。 - 由于您的任務需要使用
continue
andbreak
,這就是這里處理迭代的方式。因此,如果此檢查失敗,!currency.getName().equals(noteDetails[0])
我們只需使用continue
跳轉到下一次迭代。另外,請注意字串相等性應始終使用equals
and NEVER EVER with檢查,==
因為最后一個將執行物件相等性檢查而不是內容相等性。 - 如果貨幣是我們期望我們添加到回圈完成后回傳的總金額中的貨幣。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/494633.html
下一篇:為什么不能用反射修改字串欄位?