Java-String常用类实验

1. 编写程序,要求输入一个价格(必须是数值,值必须大于0),输入字符不合理重新输入,直到录入一个合理的价格为止,输出该价格。

提示:使用异常处理使代码更稳健。

public class PriceTest {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (true) {
			System.out.println("请输入数据:");
			try {
				double price = Double.parseDouble(sc.nextLine());
				if (price > 0) {
					System.out.println("价格为:" + price);
					break;
				} else if (price < 0) {
					System.out.println("价格不能为负!重新输入!");
				}
			} catch (Exception e) {
				System.out.println("输入的数据有问题!重新输入!");
			}
		}
	}
}

2. 某程序员编写了一个方法,该方法实现的逻辑是从传入的数组中找出最大值,但考虑到调用者可能会传入一个长度为0的数组,所以要给调用者进行相应的提示,但是Java中并没有合适的异常类名可以直观的让调用者明白出现的问题,该程序员想自己设计一个运行时异常ArrayLengthNotZeroException,为自己的方法提供服务,请帮其实现功能

@SuppressWarnings("serial")
public class ArrayLengthNotZeroException extends RuntimeException {
	public ArrayLengthNotZeroException(String message) {
		super(message);
	}

	public static int getMax(int[] arr) {
		if (arr.length == 0) {
			throw new ArrayLengthNotZeroException("数组长度不能为0!!!");
		}
		Arrays.sort(arr);
		return arr[arr.length - 1];
	}

	public static void main(String[] args) {
		int arr[] = { 1, 6, 8 };
		int max = getMax(arr);
		System.out.println("最大值为:" + max);
		int arr2[] = new int[0];
		int max2 = getMax(arr2);
		System.out.println("最大值为:" + max2);
	}
}

在这里插入图片描述

3. 键盘录入一个字符串,统计该字符串中大写字母字符、小写字母字符、数字字符出现的次数,不考虑其他字符

public class CountString {

	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);
		System.out.println("请输入一串字符串");
		int countUpper = 0;
		int countLower = 0;
		int numberDigit = 0;
		String string = input.nextLine();
		for (char ch : string.toCharArray()) {
			if (Character.isUpperCase(ch)) {
				countUpper++;
			} else if (Character.isLowerCase(ch)) {
				countLower++;
			} else if (Character.isDigit(ch)) {
				numberDigit++;
			}
		}
		System.out.println("大写字母出现的次数:" + countUpper);
		System.out.println("小写字母出现的次数:" + countLower);
		System.out.println("数字出现的次数:" + numberDigit);
	}

}

4.将输入手机号码的4到7位屏蔽后输出,如“131****9468

public class HiddenPhoneNumber {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入你的手机号:");
		String tel = sc.next();
		String before = tel.substring(0, 3);
		String after = tel.substring(7);
		String s = before + "****" + after;
		System.out.println(s);
		sc.close();
	}

}

5.给定两个字符串,A和B。A的旋转操作就是将A最左边的字符移动到最右边。例如,若A=“abcde”,在一次移动后结果就是“bcdea”。如果在若干次调整操作之后,A能变成B,那么返回true,如果不能匹配成功,则返回false

public class StringMatch {

	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入两个字符串,用,分隔");
		String[] strings = sc.nextLine().split(",");
		String string1 = strings[0];
		String string2 = strings[1];
		if (match(string1, string2)) {
			System.out.println("匹配成功!");
		} else {
			System.out.println("匹配失败");
		}
	}

	public static boolean match(String string1, String string2) {
		char[] arr = string1.toCharArray();
		int n = string1.toCharArray().length;
		// 定义指针
		int point = 1;
		while (point <= string2.length()) {
			// 把第一个数拿到 准备赋值给尾部的值
			char last = arr[0];
			for (int i = 0; i < n - 1; i++) {
				arr[i] = arr[i + 1];
			}
			arr[n - 1] = last;
			point++;
			System.out.println(arr);
			if (String.valueOf(arr).equals(string2)) {
				return true;
			}
		}
		return false;
	}
}

在这里插入图片描述

6.已知正确的用户名和密码,请用程序实现模拟用户登录。总共给三次机会,登录之后给出相应的提示

public class LoginTest {

	public static void main(String[] args) {
		for (int i = 0; i < 3; i++) {
			Scanner sc = new Scanner(System.in);
			System.out.println("请输入用户名:");
			String name = sc.nextLine();
			System.out.println("请输入密码:");
			String pwd = sc.nextLine();
			if (name.equals("zk") && pwd.equals("123")) {
				System.out.println("登录成功!");
				break;
			} else {
				if (2 - i == 0) {
					System.out.println("账户锁定!");
					break;
				} else {
					System.out.println("输入错误!重新输入,你还有" + (2 - i) + "次机会");
				}
			}
		}
	}
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值