Scanner类和String类常见对象案例

Scanner类和String类常见对象案例

需求:模拟登录, 给三次机会, 并提示还有几次。
//案例代码:
import java.util.Scanner;

public class Rng {
    public static void main(String[] args) {
        /* 需求:模拟登录, 给三次机会, 并提示还有几次。*/
        String name = "admin";
        String password = "123456";

        Scanner sc = new Scanner(System.in);
        for (int i = 1; i <= 3; i++) {
            System.out.println("请输入你的用户名");
            String uname = sc.nextLine();
            System.out.println("请输入密码");
            String pwd = sc.nextLine();
            if (uname.equals(name) && pwd.equals(password)) {
                System.out.println("登录成功");
                break;
            } else {
                if((3 - i)>0){
                    System.out.println("用户名或密码错误 你还剩余" + (3 - i) + "次机会");
                }else{
                    System.out.println("你的次数用完,账户已被冻结");
                }

            }
        }
    }
}
结果1:
	请输入你的用户名
	admin
	请输入密码
	123456
	登录成功

	Process finished with exit code 0
结果2:
	请输入你的用户名
	admi
	请输入密码
	123456
	用户名或密码错误 你还剩余2次机会
	请输入你的用户名
	admin
	请输入密码
	12345
	用户名或密码错误 你还剩余1次机会
	请输入你的用户名
	admin
	请输入密码
	12356
	你的次数用完,账户已被冻结

Process finished with exit code 0

需求:遍历字符串
//案例代码:
	public class Test {
    public static void main(String[] args) {
        String s = "时间不在于你用有多少,而在于你怎样使用";
        //遍历字符串
        for (int i = 0; i < s.length(); i++) {
            System.out.println(s.charAt(i));
        }
    }
}
结果:
时
间
不
在
于
你
用
有
多
少
,
而
在
于
你
怎
样
使
用

Process finished with exit code 0

需求:字符串反转
/*举例:键盘录入: "abc"
       反转结果: "cba"  */

public class MyTest2 {
    public static void main(String[] args) {
        举例:键盘录入 "abc"
        反转结果:"cba"*/
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一段字符串");
        String s = scanner.nextLine();
        String str="";
        for (int length = s.length()-1; length >= 0; length--) {
            char ch = s.charAt(length);
            str+=ch;
        }
        System.out.println(str);

    }
}
结果:
	abc
	cba
	
	Process finished with exit code 0
需求:把数组转换成字符串
 /*要求:将给出的字符串拼接为
    int[] arr = {1,2,3};
    拼接结果:
            "[1, 2, 3]"*/
//案例代码:
	public class Test{
   
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5,6,7,8,9,9};
        System.out.println(show(arr));
    }
    public static String show(int[] arr){
        String s="[";
        for (int i = 0; i < arr.length; i++) {
            if (i == (arr.length-1)){
                s=s+arr[i]+"]";
            }else{
                s+=arr[i]+",";
            }
        }
        return s;
    }
}

结果:
	[1,2,3,4,5,6,7,8,9,9]

Process finished with exit code 0

需求:
	查找数组中指定元素的索引值
	
案例代码:查找23在数组中第一次出现的索引值
public class demo {
    public static void main(String[] args) {
        int[] arr = {98,23,16,35,72};
        int t=show(arr);
        System.out.println(t);
    }
    public static int show(int arr[]){
        int t=0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]==23){
                t=i;
                break;
            }
        }
        return t;
    }
}

结果:
1

Process finished with exit code 0
需求:统计一个字符串中大写字母字符,小写字母字符,数字字符出现的次数。(不考虑其他字符)*/
 案例代码:
 String str="asdfASFAEZSf5244dsfsdf555asdfa36425444asdfasdf";
        int xiao=0;
        int da=0;
        int num=0;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if(ch>='a'&&ch<='z'){
                xiao++;
            }else if(ch >= 'A' && ch <= 'Z'){
                da++;
            }else{
                 num++;
            }
        }

        System.out.println("小写字符有"+xiao+"个");
        System.out.println("大写字符有" + da + "个");
        System.out.println("数字字符有" + num + "个");
    }
}
结果:
	小写字符有24个
	大写字符有7个
	数字字符有15个

	Process finished with exit code 0
需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)*/
案例代码:
public class MyTest2 {
    public static void main(String[] args) {
      /* 
        案例演示:
        需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)*/
      String str="abCDefG";
        String s = str.substring(0, 1).toUpperCase().concat(str.substring(1).toLowerCase());
        System.out.println(s); 
    }
}
结果:
Abcdefg

Process finished with exit code 0
需求:统计一个长字符串中某一个小串出现的次数
案例演1public class MyTest3 {
    public static void main(String[] args) {
        String maxStr = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";
        String minStr = "java";
        int index = maxStr.indexOf(minStr);
        //定义一个统计变量
        int count = 0;
        while (index != -1) {
            count++;
            maxStr = maxStr.substring(index + 4);
            //再次查找重新赋值索引
            index = maxStr.indexOf(minStr);
        }

        System.out.println("java 出现了 "+count+" 次");
    }
}
结果:
	java出现了 5 次
	
	Process finished with exit code 0
    
 案例演示2public class MyTest5 {
    public static void main(String[] args) {
        String maxStr = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";

        String minStr = "java";

        int length = maxStr.length();

        int length1 = maxStr.replace(minStr, "").length();

        System.out.println((length - length1) / 4);
    }
}
结果:
	5
	Process finished with exit code 0
   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值