为什么'包含'不工作?或者我做错了?

时间:2015-04-16 15:00:16

标签: java

目的:

  1. 检查用户输入是否为字母
  2. 检查它是否是元音或辅音
  3. 检查字符串中是否有重复的字母
  4. 包含不返回任何内容或我使用错误

    Scanner out = new Scanner(System.in);
        System.out.print("Please insert a text: ");
    
        String[] vowels = {"a", "e", "i", "o", "u"}; 
        String userInput = out.nextLine();
        char[] charUserInput = userInput.toCharArray();
        String temp = "";
        String temp1 = "";
        for (int i = 0; i <= charUserInput.length -1; i++){
            if (Character.isLetter(charUserInput[i])){
                if (Character.toString(charUserInput[i]).equalsIgnoreCase(vowels[0]) || (Character.toString(charUserInput[i]).equalsIgnoreCase(vowels[1]) ||
                        (Character.toString(charUserInput[i]).equalsIgnoreCase(vowels[2]) || (Character.toString(charUserInput[i]).equalsIgnoreCase(vowels[3]) || 
                                (Character.toString(charUserInput[i]).equalsIgnoreCase(vowels[4])))))){
                        if (!Character.toString(charUserInput[i]).contains(temp)){
                            temp += Character.toString(charUserInput[i]);
                        }
                }else{
                    if (!Character.toString(charUserInput[i]).contains(temp1)){
                        temp1 += Character.toString(charUserInput[i]);
                    }
    
                }
            }
        }
        System.out.println(temp);
        System.out.println(temp1);
        out.close();
    

1 个答案:

答案 0 :(得分:2)

看起来你倒置了条件:使用

temp.contains(Character.toString(charUserInput[i])) 

而不是

Character.toString(charUserInput[i]).contains(temp)