登陆

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;
	
	}

}


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值