java小程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

字符统计输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。importjava.util.Scanner;publicclassMain{publicstaticintgetEnglishCharCount(Stringstr){intlen=str.length(),num1=0;for(inta=0;alen;a++){if((str.charAt(a)='a'&&str.charAt(a)='z')||(str.charAt(a)='A'&&str.charAt(a)='Z'))num1++;}returnnum1;}publicstaticintgetBlankCharCount(Stringstr){intlen=str.length(),num2=0;for(inta=0;alen;a++){if(str.charAt(a)=='')num2++;}returnnum2;}publicstaticintgetNumberCharCount(Stringstr){intlen=str.length(),num3=0;for(inta=0;alen;a++){if(str.charAt(a)=(char)'0'&&str.charAt(a)=(char)'9')num3++;}returnnum3;}publicstaticvoidmain(String[]args){Scannerscan=newScanner(System.in);Strings=scan.nextLine();if(s.isEmpty()){System.out.println(0++0++0++0);}else{inta=getEnglishCharCount(s);intb=getBlankCharCount(s);intc=getNumberCharCount(s);intd=s.length()-a-b-c;System.out.println(a);System.out.println(b);System.out.println(c);System.out.println(d);}}}计算输入名字的漂亮度给出一个名字,该名字有26个字符串组成,定义这个字符串的“漂亮度”是其所有字母“漂亮度”的总和。每个字母都有一个“漂亮度”,范围在1到26之间。没有任何两个字母拥有相同的“漂亮度”。字母忽略大小写。给出多个名字,计算每个名字最大可能的“漂亮度”。importjava.util.Scanner;importjava.util.HashMap;importjava.util.Arrays;publicfinalclassMain{//restorecountbycharprivatestaticHashMapCharacter,IntegercharCntMap=newHashMapCharacter,Integer();//26个字母,不区分大小写privatestaticfinalintCHAR_SIZE=26;//publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);//略过第一行输入name总数scanner.nextLine();//假设输入正确,即第一行name总数与后续输入names总数数相符合Stringname=null;while(scanner.hasNextLine()){name=scanner.nextLine();System.out.println(maxNameScore(name));}scanner.close();}/***计算name最大分数*@paramname*@return*/privatestaticintmaxNameScore(Stringname){intmaxScore=0;charCntMap.clear();char[]charsName=name.toCharArray();for(inti=0;icharsName.length;i++){charopChar=Character.toLowerCase(charsName[i]);if(charCntMap.containsKey(opChar)){intvalue=charCntMap.get(opChar)+1;charCntMap.put(opChar,value);}else{charCntMap.put(opChar,1);}}Integer[]charCnt=newInteger[charCntMap.size()];charCntMap.values().toArray(charCnt);Arrays.sort(charCnt);for(inti=charCnt.length-1,j=0;i=0;i--,j++){maxScore+=(CHAR_SIZE-j)*charCnt[i];}returnmaxScore;}}学英语Jessi初学英语,为了快速读出一串数字,编写程序将数字转换成英文:如22:twentytwo,123:onehundredandtwentythree。说明:数字为正整数,长度不超过十位,不考虑小数,转化结果为英文小写;输出格式为twentytwo;非法数据请返回“error”;关键字提示:and,billion,million,thousand,hundred。方法原型:publicstaticStringparse(longnum)importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){Scannerscan=newScanner(System.in);while(scan.hasNext()){intnumber=scan.nextInt();Strings=NumberToEnglishString(number);System.out.println(s);}}publicstaticStringNumberToEnglishString(intnumber){Strings=null;if(number0){s=error;returns;}if(number20){switch(number){case0:s=zero;returns;case1:s=one;returns;case2:s=two;returns;case3:s=three;returns;case4:s=four;returns;case5:s=five;returns;case6:s=six;returns;case7:s=seven;returns;case8:s=eight;returns;case9:s=nine;returns;case10:s=ten;returns;case11:s=eleven;returns;case12:s=twelve;returns;case13:s=thirteen;returns;case14:s=fourteen;returns;case15:s=fifteen;returns;case16:s=sixteen;returns;case17:s=seventeen;returns;case18:s=eighteen;returns;case19:s=nineteen;returns;default:s=error;returns;}}if(number100){if(number%10==0){switch(number){case20:s=twenty;returns;case30:s=thirty;returns;case40:s=forty;returns;case50:s=fifty;returns;case60:s=sixty;returns;case70:s=seventy;returns;case80:s=eighty;returns;case90:s=ninety;returns;default:s=error;returns;}}else{s=NumberToEnglishString(number/10*10)+''+NumberToEnglishString(number%10);returns;}}if(number1000){if(number%100==0){s=NumberToEnglishString(number/100)+hundred;returns;}else{s=NumberToEnglishString(number/100)+hundredand+NumberToEnglishString(number%100);returns;}}if(number1000000){if(number%1000==0){s=NumberToEnglishString(number/1000)+thousand;returns;}else{s=NumberToEnglishString(number/1000)+thousand+NumberToEnglishString(number%1000);returns;}}if(number1000000000){if(number%1000000==0){s=NumberToEnglishString(number/1000000)+million;returns;}else{s=NumberToEnglishString(number/1000000)+million+NumberToEnglishString(number%1000000);returns;}}if(number999999999){if(number%1000000000==0){s=NumberToEnglishString(number/1000000000)+billion;returns;}else{s=NumberToEnglishString(number/1000000000)+billion+NumberToEnglishString(number%1000000000);returns;}}if(number999999999){s=error;returns;}returns;}}字符串加解密题目描述1、对输入的字符串进行加解密,并输出。2加密方法为:当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;其他字符不做变化。3、解密方法为加密的逆过程。接口描述:实现接口,每个接口实现1个基本操作:voidEncrypt(charaucPassword[],charaucResult[]):在该函数中实现字符串加密并输出说明:1、字符串以\0结尾。2、字符串最长100个字符。intunEncrypt(charresult[],charpassword[]):在该函数中实现字符串解密并输出说明:1、字符串以\0结尾。2、字符串最长100个字符。样例输入abcdefgBCDEFGH样例输出BCDEFGHabcdefgimportjava.util.Scanner;publicclassMain{publicstaticvoidmain(Stringargs[]){Scannerscan=newScanner(System.in);Stringstr1=scan.nextLine();Stringstr2=scan.nextLine();Stringyuanma=null,mima=null;mima=jiami(str1);yuanma=jiemi(str2);System.out.println(mima);System.

1 / 11
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功