根據(jù)題目要求,代碼如下
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名申請(qǐng)、網(wǎng)頁(yè)空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、當(dāng)陽(yáng)網(wǎng)站維護(hù)、網(wǎng)站推廣。
public?class?Test{
public?static?void?main(String[]?arg){
String?input?=?"";
int?count?=?0;
Scanner?scanner?=?new?Scanner(System.in);
input?=?scanner.next();
char[]?chs?=?input.getChars();
for(int?i=0;?ichs.length;?i++){
if(chs?==?'?'){
count++;
}
}
System.out.println("空格個(gè)數(shù)為"+count);
scanner.close();
}
}
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
StringTokenizer st = new StringTokenizer(str," ");
int total = st.countTokens();
int len[] = new int[20];
Arrays.fill(len,0);
while (st.hasMoreTokens()) {
String s = st.nextToken();
len[s.length()]++;
}
System.out.println("長(zhǎng)度 個(gè)數(shù)");
int max = 1;
for (int i = 1; i len.length; i++) {
if (len[i] 0)
max = i;
}
for (int i = 1; i
import java.io.*;
public class ReadFile{
public static void show(String s){
int len = s.length();
int letterCount = 0; //英文字母?jìng)€(gè)數(shù)
int blankCount = 0; //空格個(gè)數(shù)
int numCount = 0; //數(shù)字個(gè)數(shù)
int otherCount = 0; //其他字符個(gè)數(shù)
for(int i = 0;i len;i ++){
char tem = s.charAt(i);
if((tem 'A' tem 'Z') || (tem 'a' tem 'z'))//英文字母
letterCount ++;
else if(tem == ' ')//空格
blankCount ++;
else if(tem '0' tem '9')//數(shù)字
numCount ++;
else//其他
otherCount ++;
}
System.out.println("英文字母?jìng)€(gè)數(shù): " + letterCount);
System.out.println("空格個(gè)數(shù): " +blankCount);
System.out.println("數(shù)字個(gè)數(shù): " + numCount);
System.out.println("其他字符個(gè)數(shù): " + otherCount);
}
public static void main(String[] args)throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("請(qǐng)輸入您要統(tǒng)計(jì)的字符串: ");
String str = br.readLine();
show(str);
}
}
package exercise;
import java.util.Scanner;
public class Exer14 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("輸入一行字符:");
String str=input.nextLine();
int n=0;//數(shù)字統(tǒng)計(jì)
int ch=0;//字母統(tǒng)計(jì)
int s=0;//空格統(tǒng)計(jì)
int q=0;//其他統(tǒng)計(jì)
for(int x=0;xstr.length();x++) {
char c=str.charAt(x);
if(c='0'c='9') {
n++;
}
else if((c='a'c='z')||(c='A'c='Z')) {
ch++;
}
else if(Character.isSpace(c)) {
s++;
} else{
q++;
}
}
System.out.println("數(shù)字的個(gè)數(shù)為:"+n);
System.out.println("字母的個(gè)數(shù)為:"+ch);
System.out.println("空格的個(gè)數(shù)為:"+s);
System.out.println("其他的字符個(gè)數(shù)為:"+q);
}
}
123456789101112131415161718192021222324
public?static?void?main(String[]?args)?throws?IOException?{????????BufferedReader?br=new?BufferedReader(new?InputStreamReader(System.in));????????String?str=br.readLine();?????????int?countNum?=?0;//統(tǒng)計(jì)數(shù)字的個(gè)數(shù)????????int?countChar?=?0;//統(tǒng)計(jì)英文字母的個(gè)數(shù)????????int?countSpace?=?0;//統(tǒng)計(jì)空格的個(gè)數(shù)????????int?countOthers?=?0;//統(tǒng)計(jì)其它字符的個(gè)數(shù)????????for?(int?i?=?0;?i??str.length();?i++)?{????????????char?c?=?str.charAt(i);????????????if?(c?=?'0'??(int)?c?=?'9')?{????????????????countNum++;????????????}?else?if?((c?=?'a'??c?=?'z')||(c?=?'A'??c?=?'Z'))?{????????????????countChar++;????????????}?else?if?(c?==?'?')?{????????????????countSpace++;????????????}?else{????????????????countOthers++;????????????}????????}????????System.out.println("數(shù)字個(gè)數(shù):"+countNum);????????System.out.println("英文字母?jìng)€(gè)數(shù):"+countChar);????????System.out.println("空格個(gè)數(shù):"+countSpace);????????System.out.println("其他字符個(gè)數(shù):"+countOthers);????}
一、問(wèn)題分析:
輸入一行字母,那么會(huì)以換行結(jié)束。所以可以存入數(shù)組,也可以逐個(gè)輸入,遇到換行結(jié)束。
要統(tǒng)計(jì)各個(gè)類(lèi)的個(gè)數(shù),就要逐個(gè)判斷是哪個(gè)分類(lèi)的。
由于在ASCII碼中,數(shù)字,大寫(xiě)字母,小寫(xiě)字母分別連續(xù),所以可以根據(jù)邊界值判斷類(lèi)型。
二、算法設(shè)計(jì):
1、讀入字符,直到遇到換行結(jié)束。
2、對(duì)于每個(gè)字符,判斷是字母還是數(shù)字,或者空格,或者是其它字符。
3、對(duì)于每個(gè)字符判斷后,對(duì)應(yīng)類(lèi)別計(jì)數(shù)器自加。
4、最終輸出結(jié)果。
import?java.util.Scanner;
public?class?F?{
public?static?void?main(String[]?args)?{
//空格數(shù)
int?space?=?0;
//換行數(shù)
int?line?=?0;
Scanner?s?=?new?Scanner(System.in);
String?str?=?"-1";
while?(!"t".equalsIgnoreCase(str))?{
str?=?s.nextLine();
line++;
char[]?ch?=?str.toCharArray();
for?(int?i?=?0;?i??ch.length;?i++)?{
if?(Character.isSpaceChar(ch[i]))?{
space++;
}
}
}
line=line-2;
System.out.println(space+"?"+line);
}
}
控制臺(tái)輸出結(jié)果: