所以,我輸入了兩個字符串,並且在mString中查找了subString。當我將該方法更改爲布爾值時,它將返回true或false的正確輸出(通過在contains語句中使用返回值)。返回使用'contains'比較兩個字符串的結果
我不知道如何使用該語句來檢查包含運算符的結果。我已經完成了以下工作。
public class CheckingString
{
public static void main(String[] args)
{
// adding boolean value to indicate false or true
boolean check;
// scanner set up and input of two Strings (mString and subString)
Scanner scan = new Scanner(System.in);
System.out.println("What is the long string you want to enter? ");
String mString = scan.nextLine();
System.out.println("What is the short string that will be looked for in the long string? ");
String subString = scan.nextLine();
// using the 'contain' operator to move check to false or positive.
// used toLowerCase to remove false negatives
check = mString.toLowerCase().contains(subString.toLowerCase());
// if statement to reveal resutls to user
if (check = true)
{
System.out.println(subString + " is in " + mString);
}
else
{
System.out.println("No, " + subString + " is not in " + mString);
}
}
}
有沒有辦法讓檢查字段正常工作以返回if-else語句中的值?
衛生署...我無法相信我錯過了。很高興知道布爾if-statmenet。 – user1588867
足夠常見,我馬上發現它:-)(很多學生都這麼做)。至少只有布爾值纔會發生。 – TofuBeer