Java初学者作业——编写Java程序,实现判断所输入字符的类型(数字、小写字母、大写字母或其他字符)

返回本章节

返回作业目录


需求说明:

编写Java程序,实现判断所输入字符的类型(数字、小写字母、大写字母或其他字符)

实现思路:

  1. 声明变量c,用于存储用户输入的字符。
  2. 通过Scanner接收用户输入的字符,并为变量c赋值。
  3. 根据字符的特点,使用多重if结构实现各种字符类型的判断。
  4. 使用System.out.println()实现格式化输出运算结果。

字符区间

条件表达式

所属字符类型

‘A’——‘Z’

c >= ‘A’ && c <= ‘Z’

大写字母

‘a’——‘z’

c >= ‘a’ && c <= ‘Z’

小写字母

‘0’ ——’9’

c >= ‘0’ && c <= ‘9’

数字

实现代码:

import java.util.Scanner;

public class CharType {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in); 
		System.out.println("请输入一个字符:");
		char c = sc.next().charAt(0);
		//判断该字符的类型
		if(c >='a'&&c <='z'){
			System.out.println("该字符为小写字母");
		}else if(c >='A'&&c <='Z'){
			System.out.println("该字符为大写字母");
		}else if(c >='0'&&c <='9'){
			System.out.println("该字符为数字");
		}else{
			System.out.println("该字符为其他类型");
		}
	}
}

 

posted @ 2020-02-18 13:23  明金同学  阅读(238)  评论(0编辑  收藏  举报