1.用数组实现登陆
public class dengluSZ {
public static void main(String[] args) {
char[] a={'a','b','c','d'};
int[] b={1,2,3,4};
char x='e';
int y=4;
int i;
for(i=0;i<a.length;i++){
if(x==a[i]){
if(y==b[i]){
System.out.println("登陆成功");
}
else{
System.out.println("姓名与学号不符");
}
break;
}
}
if(i==a.length)
System.out.println("未找到该学生");
}
}
2.用类实现登陆
public class dengluLEI {
public static void main(String[] args) {
dengluLEI cg=new dengluLEI();
String x1=cg.Chenggong('a',1);
String x2=cg.Chenggong('b',1);
String x3=cg.Chenggong('e',1);
System.out.println(x1);
System.out.println(x2);
System.out.println(x3);
}
public String Chenggong(char x,int y){
char[] a={'a','b','c','d'};
int[] b={1,2,3,4};
int i;
String result="";
for(i=0;i<a.length;i++){
if(x==a[i]){
if(y==b[i]){
result="登陆成功";
}
else{
result="密码错误";
}
break;
}
}
if(i==a.length)
result="未找到该用户";
return result;
}
}
3.结构优化
一共有两种结果
if————else
设默认值为其中一种,干掉else
public class dengluLEI2 {
public static void main(String[] args) {
dengluLEI cg=new dengluLEI();
String x1=cg.Chenggong('a',1);
String x2=cg.Chenggong('b',1);
String x3=cg.Chenggong('e',1);
System.out.println(x1);
System.out.println(x2);
System.out.println(x3);
}
public String Chenggong(char x,int y){
char[] a={'a','b','c','d'};
int[] b={1,2,3,4};
int i;
String result="密码或用户名错误";
for(i=0;i<a.length;i++){
if(x==a[i]){
if(y==b[i]){
result="登陆成功";
}
break;
}
}
return result;
}
}