在if语句和while循环中使用equalsIgnoreCase

时间:2017-03-27 11:17:10

标签: if-statement do-while equals-operator

    String wrd;

    do
    {
        wrd = JOptionPane.showInputDialog("Enter a word");
        if (!wrd.equalsIgnoreCase("last"))
        {
            if (wrd.contains("s") || wrd.contains("S"))
            {
                System.out.println(wrd + " does contain 's' or 'S'");
            }
            else
            {
                System.out.println(wrd + " doesn't contain 's' or 'S'");
            }
        }
    }
    while (!wrd.equalsIgnoreCase("last"));
    System.out.println("Program Ended");

该程序基本上允许用户输入任何单词,直到他/她键入单词“last”。

如果单词包含字母's',则应显示单词+包含's'或'S'否则它应显示单词+不包含's'或'S'。

示例:user =

输入的单词

•约翰

•詹姆斯

•馅饼

•莎莉

结果:

John不包含's'或'S'

詹姆斯确实包含's'或'S'

Pie不包含's'或'S'

Sally不包含's'或'S'

该程序有效但我需要更改if语句,以便它使用equalsIgnoreCase方法而不是

来识别大小写的's'
    wrd.contains("s") || wrd.contains("S")

1 个答案:

答案 0 :(得分:0)

试试这个

String wrd;
    do {
         wrd = JOptionPane.showInputDialog("Enter a word s");
        if (wrd.toLowerCase().contains("s")) {
            System.out.println(wrd + " does contain 's' or 'S'");
        } else {
            System.out.println(wrd + " doesn't contain 's' or 'S'");
        }
    } while (!wrd.equalsIgnoreCase("last"));
    System.out.println("Program Ended");