我需要創建一個方法,使用字串字符計算字串 str 中發生的次數,并且我需要通過這些測驗,這是我的任務,而不是我的代碼:
@Test
public void test_count_bothEmptyString() {
int expected = 0;
int actual = CCStringsIfAndWhile.count("", "");
assertEquals("Testing count - both empty string", expected, actual);
}
@Test
public void test_count_firstEmptyString() {
int expected = 0;
int actual = CCStringsIfAndWhile.count("", "abcdefghijklmnopqrstuvwxyz");
assertEquals("Testing count - first is empty string", expected, actual);
}
@Test
public void test_count_secondEmptyString() {
int expected = 0;
int actual = CCStringsIfAndWhile.count("This is a test", "");
assertEquals("Testing count - second is empty string", expected, actual);
}
@Test
public void test_count_one() {
int expected = 1;
int actual = CCStringsIfAndWhile.count("This is a test", "abc");
assertEquals("Testing count - 'This is a test', 'abc'", expected, actual);
}
@Test
public void test_count_many() {
int expected = 6;
int actual = CCStringsIfAndWhile.count("This is a test", "sapqi");
assertEquals("Testing count - 'This is a test', 'sapqi'", expected, actual);
}
@Test
public void test_count_upperAndLowerCase() {
int expected = 7;
int actual = CCStringsIfAndWhile.count("This is another test", "stpq");
assertEquals("Testing count - 'This is another test', 'stpq'", expected, actual);
}
我已經嘗試過這樣的事情,但是我似乎找不到檢查字串每個字母的 if 陳述句:
public static int count(String str, String chars)
{
int count = 0;
int charCount = 0;
while(count<str.length())
{
if()
{
charCount ;
return charCount ;
}
else {
charCount=0;
}
count ;
}
return charCount;
}
uj5u.com熱心網友回復:
可能應該使用兩個回圈,因為您需要遍歷兩個字串以查找一個字串在另一個字串中出現的所有字符
return charCount ;肯定不對,回圈里面的return會提前停止回圈根據您的測驗用例判斷,您需要添加一個 if 陳述句來檢查任一輸入中的空字串并回傳 0
在上面實作你會得到這樣的東西
if(str.isEmpty()||chars.isEmpty()){return 0;}
for (char strChar: str.toCharArray()) {
for (char chars: chars.toCharArray()) {
if(strChar==chars){
charCount ;
}
}
}
uj5u.com熱心網友回復:
這就是我所做的,它成功了,謝謝你的幫助:
public static int count(String str, String chars)
{
int charCount = 0;
if(str.isEmpty()||chars.isEmpty())
{
return 0;
}
for (char strC: str.toCharArray())
{
for (char charsC: chars.toCharArray())
{
if(Character.toLowerCase(strC)==Character.toLowerCase(charsC))
{
charCount ;
}
}
}
return charCount;
uj5u.com熱心網友回復:
以下是我的建議。您可以通過使用StringAPI的幾種方法來做到這一點
- 讓變數
count索引中的一個字符chars。您可以使用charAt (count)索引單個字符。 - 這意味著您的
while回圈將運行 whilecount < chars.length,而不是str.length. - 您將需要一個內部回圈,
str以防chars. - 您可以使用一種
indexOf方法從 中搜索str當前字符chars。 - 跟蹤 中的當前搜索起始位置,并在回傳非負數
str時更新它以進行下一次搜索。indexOf - 您可以使用
indexOf獲取起始位置的方法或使用substring方法。 - 我不認為你想要
else { charCount=0;}。您想要總數,因此每次找到匹配項時,都會增加charCount. 您不會將其設定為零,除非在您執行的回圈之前。
其他建議:
- 在我看來,在有回圈的
for地方使用while回圈會使代碼更具可讀性。當迭代次數在回圈開始時確定時,我通常使用for回圈,這在您的程式中似乎就是這種情況。當迭代次數不確定時,我使用 awhile或 a 。do ... while
--
- 您可以創建一個輔助方法來計算字串中某個字符的出現次數。在回圈期間,對于 中的每個字符
chars,呼叫輔助方法。 - 輔助方法可能看起來像
public static int charCounter (String str, char c). 您的count方法將有一個回圈,并且charCounter將有一個回圈。 - 您可以使用
toCharArray將 a 轉換String為char [].
--
- 一個更好的名字
count可能是charsIndex。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/510257.html
標籤:爪哇细绳
上一篇:如何在多個字串周圍添加單引號
