首页 > 解决方案 > 如何制作一个java程序来检查字符串中是否有元音?

问题描述

我制作了一个程序来检查字符串中是否至少有一个元音。但是当我输入一个包含元音的字符串时,结果是“你的密码字不可接受”而不是“你的密码字是可以接受的”。

有人可以告诉我我哪里做错了吗?谢谢你!

这是程序:

import java.util.Scanner;
public class checker {
    static Scanner input= new Scanner(System.in);
    public static void main(String[] args) {
        String password;
        System.out.println("enter your password:");
        password= input.next();
        String vowel[]= {"a","e","i","o","u"};
        for(int i=0; i<5;i++) {
            boolean check[] = new boolean[5];
            check[i]=password.contains(vowel[i]);
             if(i==vowel.length-1&&check[0] ==false && check[1]==false && check[2]==false && check[3]==false && check[4]==false) {
                System.out.println("your password word is not acceptable");
             }else System.out.println("your password is acceptable");
          }
      }
 }

标签: javaarrays

解决方案


您可以通过以下方式执行此操作:

import java.util.Scanner;
public class checker {
    static Scanner input= new Scanner(System.in);
    public static void main(String[] args) {
        String password;
        System.out.println("enter your password:");
        password= input.next();
        String vowel[]= {"a","e","i","o","u"};
        boolean check = false;
        for(int i=0; i<5;i++) {
            check = password.toLowercase().contains(vowel[i]);
            if(check){ 
                break; 
            }
       }
             if(!check){
                System.out.println("your password word is not acceptable");
             }else {
                System.out.println("your password is acceptable");
             }
      }
 }

推荐阅读