数组的应用练习

数组的应用练习

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

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

    
}



转载于:https://www.cnblogs.com/mt1500/p/4467518.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值