java判斷各類型字符個數(shù)實例代碼
描述
輸入一行字符串,分別統(tǒng)計出其中英文字母、空格、數(shù)字和其它字符的個數(shù)
輸入描述:
控制臺隨機輸入一串字符串
輸出描述:
輸出字符串中包含的英文字母個數(shù),數(shù)字個數(shù),空格個數(shù),其它字符個數(shù)(格式為:英文字母x數(shù)字x空格x其他x),預設(shè)代碼中已給出輸出.
import java.util.Scanner; public class Main { public static void main(String[] args) { int numbers = 0; int words = 0; int space = 0; int other = 0; Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); //write your code here...... System.out.println("英文字母"+words+"數(shù)字"+numbers+"空格"+space+"其他"+other); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { int numbers = 0; int words = 0; int space = 0; int other = 0; Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); //write your code here...... for(int i=0;i<str.length();i++){ char c=str.charAt(i); if((c>='a' && c<='z')|| (c>='A' && c<='Z')){ words++; continue; } if(c>='0' && c<='9' ){ numbers++; continue; } if(c==' '){ space++; continue; } else{ other++; continue; } } System.out.println("英文字母"+words+"數(shù)字"+numbers+"空格"+space+"其他"+other); } }
注意:每次計數(shù)完后要跳出循環(huán),否則就取出的字符會挨個問一遍判斷語句 出現(xiàn)問題?
到此這篇關(guān)于java判斷各類型字符個數(shù)實例代碼的文章就介紹到這了,更多相關(guān)java判斷字符個數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java的MyBatis框架項目搭建與hellow world示例
MyBatis框架為Java程序的數(shù)據(jù)庫操作帶來了很大的便利,這里我們就從最基礎(chǔ)的入手,來看一下Java的MyBatis框架項目搭建與hellow world示例,需要的朋友可以參考下2016-06-06SpringBoot使用JTA實現(xiàn)對多數(shù)據(jù)源的事務(wù)管理
了解事務(wù)的都知道,在我們?nèi)粘i_發(fā)中單單靠事務(wù)管理就可以解決絕大多數(shù)問題了,但是為啥還要提出JTA這個玩意呢,到底JTA是什么呢?他又是具體來解決啥問題的呢?本文小編就給大家介紹一下如何在Spring Boot中使用JTA實現(xiàn)對多數(shù)據(jù)源的事務(wù)管理2023-11-11springboot 自定義LocaleResolver實現(xiàn)切換語言
我們在做項目的時候,往往有很多項目需要根據(jù)用戶的需要來切換不同的語言,使用國際化就可以輕松解決。這篇文章主要介紹了springboot 自定義LocaleResolver切換語言,需要的朋友可以參考下2019-10-10Spring中@ControllerAdvice注解的用法解析
這篇文章主要介紹了Spring中@ControllerAdvice注解的用法解析,顧名思義,@ControllerAdvice就是@Controller 的增強版,@ControllerAdvice主要用來處理全局數(shù)據(jù),一般搭配@ExceptionHandler、@ModelAttribute以及@InitBinder使用,需要的朋友可以參考下2023-10-10