展开全部
题目分析:分析题目要求,应该从标准输入接受一个小写字母;
对法输入进行合法性62616964757a686964616fe59b9ee7ad9431333363376461判断。对于非法输入显示错误信息,提示用户重新输入;编码思路读取标准输入使用Scanner,并且在使用完在finally中进行close
因为非法输入需要重新输入,所以使用while循环。在非法输入时,打印提示信息,并用continue重新接受输入。在合法输入时,进行字母大小写转换,并用break跳出循环;
因为字母的ASCII码整数表示是按照字母顺序连续的,所以可以使用差值的方法进行小写字母转换大写字母。示例代码public static void main(String[] args) {
Scanner scanner = null;
try {
while (true) {
System.out.println("请输入一个小写字母:");
scanner = new Scanner(System.in);
String line = scanner.nextLine();
char[] input = line.toCharArray();
if (input.length != 1) {
System.out.println("输入长度有误,请重新输入!");
continue;
} else {
char lower = input[0];
if (lower > 'a' && lower
char upper = (char) (lower - 'a' + 'A');
System.out.println(lower + "的大写字母是" + upper);
break;
}else{
System.out.println("输入的不是小写字母,请重新输入!");
continue;
}
}
}
} finally {
if (scanner != null)
scanner.close();
}
}