返回使用'contains'比较两个字符串的结果

use*_*867 0 java if-statement boolean

所以,我输入了两个字符串,并且在mString中查找了subString.当我将方法更改为boolean时,它返回true或false的正确输出(通过在contains语句上使用return).

我不知道如何使用该语句来检查包含运算符的结果.我完成了以下工作.

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);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

有没有办法使该检查字段正常工作以在if-else语句中返回值?

Tof*_*eer 5

if (check = true){
Run Code Online (Sandbox Code Playgroud)

应该:

if (check == true){
Run Code Online (Sandbox Code Playgroud)

通常你会写:

if(check)
Run Code Online (Sandbox Code Playgroud)

检查是否真实

和:

if(!(check)) 
Run Code Online (Sandbox Code Playgroud)

要么:

如果(!检查)

检查是否错误.


PC.*_*PC. 5

琐碎的错误:

更改if(check = true)if(check == true)或只是if (check)

通过执行check = true您分配true来检查所以条件if(check = true)将始终为真.