zoukankan      html  css  js  c++  java
  • 数组的应用练习

    数组的应用练习

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

    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("
    你输入错误次数太多");
    		}
    		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"));
    	}
    
    	
    }
    
    
    
    
    
  • 相关阅读:
    .NET Interop 工具集
    关于正弦波的算法
    Windows Phone 系列 本地数据存储
    Xaml cannot create an instance of “X”
    Windows Phone 系列 使用 MVVM绑定时无法获取当前值
    Windows Phone 系列 应用程序图标无法显示
    Windows Phone 系列 WPConnect无法上网的问题
    Windows Phone 系列 使用 Windows Phone 保存铃声任务
    WP7.5提交应用
    Windows Phone 系列 动态删除ObservableCollection
  • 原文地址:https://www.cnblogs.com/mt1500/p/4467518.html
Copyright © 2011-2022 走看看