问题描述
我正在尝试创建一个方法来检查Login(用户名和密码)是否至少包含6个字符。
I'm trying to create a method which checks if the Login (username and password) has a minimum of 6 charakters.
要意识到我创建了这个方法 public void checkLoginData(final String username,final String password)
。在那个方法中,我创建了booleans(用户和pass),我可以创建4个不同的布尔链:
To realize that I created this method public void checkLoginData(final String username, final String password)
. In that method, I create to booleans (user and pass), with those I can create 4 different boolean-chains:
- user:true pass:true
- user:false pass:true
- user:false pass:false
- user: true pass:false
- user: true pass: true
- user: false pass: true
- user: false pass: false
- user: true pass: false
现在我想为每个人做一个切换/案例请求,但我没有得到如何实现...
Now I'd like to do a switch/case request for each of them, but I don't get how to realize that...
如果你问我为什么需要这个开关,我只是觉得我需要它,因为我想为这些4布尔链,它表示/显示不同的东西。另外,我想用性感的java方式做这件事,而不是成千上万的不同'ifs':P,请帮助!
If you ask why I need the switch, I just think I need it, because I'd like to do for every of those 4 boolean-chains, that it does/show something diffrent. Also I'd like to do this in a sexy-java-way not with tousands of diffrent 'ifs' :P, Please help!
这是方法的代码:
public void checkLoginData(final String username, final String password){
boolean user, pass;
if (username.length() < 6){
user = false;
}else {
user = true;
}
if (password.length() < 6){
pass = false;
}else {
pass = true;
}
boolean[] logindaten = {user, pass};
}
提前帮助的Thx!
Thx for the help in Advance!
最好的问候safari
Best Regards safari
推荐答案
你不能只在整数类型上切换 boolean []
。要将布尔值转换为int,您可以使用2位布尔值的位掩码,例如:
You can't switch over boolean[]
, only over integral types. To convert the booleans to an int, you could use a bit mask for the 2 booleans, like for example this:
int val = 0;
if (user) val |= 0x1;
if (pass) val |= 0x2;
switch (val) {
case 0: // Both too short
case 1: // User Ok, pass too short
case 2: // User too short, pass ok
case 3: // Both Ok
}
这篇关于使用布尔值的switch / case请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!