【问题标题】:Using equalsIgnoreCase in if statement and while loop在 if 语句和 while 循环中使用 equalsIgnoreCase
【发布时间】:2017-08-20 01:45:36
【问题描述】:
    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”。

示例:用户输入的单词=

• 约翰

• 詹姆斯

• 饼图

• 莎莉

结果:

John 不包含“s”或“S”

James 确实包含 's' 或 'S'

饼图不包含“s”或“S”

Sally 不包含“s”或“S”

程序可以运行,但我需要更改 if 语句,以便它使用 equalsIgnoreCase 方法而不是使用来识别小写和大写的 's'

    wrd.contains("s") || wrd.contains("S")

【问题讨论】:

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


    【解决方案1】:

    试试这个

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

    【讨论】:

      猜你喜欢
      • 2013-06-09
      • 2016-01-12
      • 2015-07-06
      • 2012-06-18
      • 2015-05-19
      • 2012-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多