java - 如何制作一个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");
}
}
}
解决方案
您可以通过以下方式执行此操作:
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");
}
}
}
推荐阅读
- python-3.x - 管道中的 fit 与 fit_transform
- python - TypeError:向 https 网站提交凭据时需要一个类似字节的对象,而不是“str”
- json - 为 Google CloudML 部署创建与 TensorFlow 中的训练分开的服务图?
- python - Python 3.7 anaconda 环境-import _ssl DLL 加载失败错误
- python - 无需下载即可查找在线视频的视频尺寸
- react-native - testflight 会清除本地数据吗?
- ios - 如何在 UIActivityViewController 中只保留空投选项?
- sql - U-SQL 是否接受使用多个 FIRST_VALUE 来删除特定列中的重复项?
- javascript - 纹理缓存溢出:16 > 可用的纹理单元
- google-app-engine - 将 Datastore Admin 映射到不同的 URL?