数组的应用练习

数组的应用练习

输入用户名和密码,验证用户身份

package mt;

import java.util.Scanner;

public class Test_MiMa {
	
public static int panDuan(String[] a , String b){
	for (int i = 0; i < a.length; i++) {
		if (b.equals(a[i])) {
			return i;
		}
	}
	return -1;	
}	
	
	public static void main(String[] args) {
		String[] user = {"admin","mt","guset"};
		String[] miMa = {"123456","abcdef","333333"};
		Scanner sc = new Scanner(System.in);
		boolean p = true;  int i = 1;
		do {  i++;
			System.out.print("请输入用户名:");
			String name = sc.next().trim();
			System.out.print("请输入密码:");
			String nmb = sc.next().trim();
			int j = panDuan(user,name);
			if (j==-1) {
				System.out.println("你输入的密码或用户名有错!");
			}
			else if (nmb.equals(miMa[j])) {
				System.out.println("欢迎登录·····");
				p = false;
			} else {
				System.out.println("你输入的密码或用户名有错!");		
			}
		} while (p && i<=3 );
		if (i>3) {
			System.out.println("\n你输入错误次数太多");
		}
		sc.close();
	}
	
}


实现字符串倒转、字符串去掉空格、字符串大小写互换的方法

package mt;

public class Test_Dao {
	/**
	 * 倒转字符串
	 * @param str
	 * @return
	 */
	public static String reverse(String str){		
		String newStr = ""; 
		for (int i = str.length()-1; i >= 0; i--) {
			newStr += str.charAt(i);
		}
		return newStr;
	}
	/**
	 * 去字符串中的空字符
	 * @param str
	 * @return
	 */
	public static String trimall(String str){
		String newStr = "";
		for (int i =0 ; i <=str.length()-1; i++) {
			
			if (str.charAt(i) != ' ') {
				newStr += str.charAt(i);	
			}
			
		}
		return newStr;
	}
	
	/**
	 * 大小写互换
	 * @param str
	 * @return
	 */
	
	public static String switchupperlower(String str) {
		String newstr = "";
		for (int i = 0; i < str.length(); i++) {
			char a = str.charAt(i);
			if (a >= 'A' && a <= 'Z') {
				a += 32;    //强制转换为小写  a = (char)(a+32)
			}
			else if (a >= 'a' && a <= 'z') {
				a -= 32;       //强制转换为大写
			}
			newstr += a;
		}
		
		return newstr;
	}
	
	
	public static void main(String[] args) {
		System.out.println(reverse("hello"));
		System.out.println(reverse("i love you"));
		System.out.println(reverse("loac"));
		
		System.out.println(trimall("h  ll  o e  kjddh   dd"));
		
		System.out.println(switchupperlower("Hello  Word"));
	}

	
}




posted @ 2015-04-29 21:54  乜天  阅读(157)  评论(0编辑  收藏  举报