java判斷各類型字符個數實例代碼
更新時間:2021年12月29日 10:49:13 作者:小孫的代碼分享
大家好,本篇文章主要講的是java判斷各類型字符個數實例代碼,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
描述
輸入一行字符串,分別統(tǒng)計出其中英文字母、空格、數字和其它字符的個數
輸入描述:
控制臺隨機輸入一串字符串
輸出描述:
輸出字符串中包含的英文字母個數,數字個數,空格個數,其它字符個數(格式為:英文字母x數字x空格x其他x),預設代碼中已給出輸出.
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+"數字"+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+"數字"+numbers+"空格"+space+"其他"+other); } }
注意:每次計數完后要跳出循環(huán),否則就取出的字符會挨個問一遍判斷語句 出現問題?
到此這篇關于java判斷各類型字符個數實例代碼的文章就介紹到這了,更多相關java判斷字符個數內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java的MyBatis框架項目搭建與hellow world示例
MyBatis框架為Java程序的數據庫操作帶來了很大的便利,這里我們就從最基礎的入手,來看一下Java的MyBatis框架項目搭建與hellow world示例,需要的朋友可以參考下2016-06-06springboot 自定義LocaleResolver實現切換語言
我們在做項目的時候,往往有很多項目需要根據用戶的需要來切換不同的語言,使用國際化就可以輕松解決。這篇文章主要介紹了springboot 自定義LocaleResolver切換語言,需要的朋友可以參考下2019-10-10Spring中@ControllerAdvice注解的用法解析
這篇文章主要介紹了Spring中@ControllerAdvice注解的用法解析,顧名思義,@ControllerAdvice就是@Controller 的增強版,@ControllerAdvice主要用來處理全局數據,一般搭配@ExceptionHandler、@ModelAttribute以及@InitBinder使用,需要的朋友可以參考下2023-10-10