JAVA学习记录(第七周)20220117-20220123

154 阅读1分钟

2022年1月17日

编制UTILITY文件

import java.util.Scanner;
/**
将不同的功能封装为方法,就可以直接通过调用方法使用它的功能,而无需考虑具体功能实现细节。
*/
public class Utility
{
	private static Scanner scanner = new Scanner(System.in);
	/**用于界面菜单的选择。该方法读取键盘,如果用户键入'1'-'4'的任意字符,则方法返回。返回值为用户键入字符。
	*/
	public static char MenuSelection()
	{
		char c;
		for(;;)
		{
			String str = readKeyBoard(1);
			c = str.charAt(0);
			if(c != '1' &&  c != '2' && c != '3' && c != '4')
			{
				System.out.print("选择错误,请重新输入:");
			}
			else
			{
				break;
			}
		}
		return c;
	}