我在这里的输入需要不区分大小写。我将向您展示我的代码,请帮助,我对如何做到这一点一无所知[重复]

Posted

技术标签:

【中文标题】我在这里的输入需要不区分大小写。我将向您展示我的代码,请帮助,我对如何做到这一点一无所知[重复]【英文标题】:My input here need to be not case-sensitive. I'll show you my code please help, I have zero idea how to do it [duplicate] 【发布时间】:2021-06-03 04:07:42 【问题描述】:

我在这里的输入需要不区分大小写。我将向您展示我的代码,请帮助,我对如何做到这一点一无所知。我是否需要重新编写代码使其不区分大小写?

public static void main(String[] args) 
        
        Scanner sc = new Scanner(System.in);

        String s, x;

        System.out.print("Enter a sentence: ");
        s = sc.nextLine();

        char[] c = s.toCharArray();
        int vowel=0;

        for (int i = 0; i < s.length(); i++) 

            if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' || s.charAt(i)=='o' || s.charAt(i)=='u')
                vowel++;
        

        System.out.println("There are "+ vowel + " vowels in the sentence");

        System.out.print("\nEnter a substring that you want to search: ");
        x = sc.nextLine();
        boolean check = s.contains(x);

        if (check)
            System.out.println("There is a substring '" + (x.toUpperCase()) + "' in the sentence");

        
        else
            System.out.println("There is no substring '" + x + "' in the sentence");
        
        System.out.println("\nThe entered sentence in uppercase characters.");
        System.out.println(s.toUpperCase());
    

【问题讨论】:

只需在要比较的两个字符串上调用toLowerCase()toUpperCase() 欺骗:How to check if a String contains another String in a case insensitive manner in Java? 【参考方案1】:

应该使字符串完全小写或大写。在接受输入时使用它:

s = sc.nextLine().toLowerCase();

还有:

x = sc.nextLine().toLowerCase();

完整的main方法:

public static void main(String[] args) 

        Scanner sc = new Scanner(System.in);

        String s, x;

        System.out.print("Enter a sentence: ");

        s = sc.nextLine().toLowerCase();

        int vowel=0;

        for (int i = 0; i < s.length(); i++) 

            if(s.charAt(i)=='a' || s.charAt(i)=='e' || s.charAt(i)=='i' || s.charAt(i)=='o' || s.charAt(i)=='u')
                vowel++;
        

        System.out.println("There are "+ vowel + " vowels in the sentence");

        System.out.print("\nEnter a substring that you want to search: ");

        x = sc.nextLine().toLowerCase();

        boolean check = s.contains(x);

        if (check)
            System.out.println("There is a substring '" + (x.toUpperCase()) + "' in the sentence");

        
        else
            System.out.println("There is no substring '" + x + "' in the sentence");

        System.out.println("\nThe entered sentence in uppercase characters.");
        System.out.println(s.toUpperCase());
    

【讨论】:

以上是关于我在这里的输入需要不区分大小写。我将向您展示我的代码,请帮助,我对如何做到这一点一无所知[重复]的主要内容,如果未能解决你的问题,请参考以下文章

4个非常有用的 Flutter 技巧

UIPageViewControllers UIPageControl 在过渡期间出现故障

PHP加载3D模型WebGL

Flutter 构建一个完整的聊天应用程序

无法停止 SQL 注入错误

如何在 BottomSheetDialogFragment 中应用屏幕调整大小?