用java语言编写一个程序,输出一个字符串中的大写字母数。 java,编写程序,从命令行读入字符串,显示该字符串中大写字...
\u7f16\u5199Java\u7a0b\u5e8f\uff0c\u5c06\u4e00\u5b57\u7b26\u4e32\u5176\u4e2d\u7684\u5927\u5c0f\u5199\u5b57\u6bcd\u7684\u5b57\u7b26\u5206\u522b\u8f93\u51fa\u5faa\u73af\u5b57\u7b26\u4e32\u7528charAt\u65b9\u6cd5\u6765\u83b7\u53d6\u5b57\u7b26\uff0c\u5c06\u5b57\u7b26\u8f6c\u4e3aint\uff0c\u53ea\u8981\u5224\u65ad\u8f6c\u6362\u540e\u7684\u6570\u503c\u5c31\u53ef\u4ee5\u5224\u65ad\u5927\u5c0f\u5199\u5b57\u6bcd\uff0c\u5927\u5199\u5b57\u6bcd\u7684\u8303\u56f4\u662f65-90\uff0c\u5c0f\u5199\u5b57\u6bcd\u7684\u8303\u56f4\u662f97-122\u3002
public class Main {
public static void main(String[] args) {
String s = "aaBvdEFdsd";
StringBuffer ss = convertString(s);
System.out.println("\u5927\u5c0f\u5199\u8f6c\u6362\u540e\u662f:" + convertString(s));
System.out.println("\u5012\u5e8f\u8f93\u51fa\u540e\u662f:" + ss.reverse());
}
public static StringBuffer convertString(String str) {
String upStr = str.toUpperCase();
String lowStr = str.toLowerCase();
StringBuffer buf = new StringBuffer(str.length());
for (int i = 0, k = str.length(); i < k; i++) {
{
if (str.charAt(i) == upStr.charAt(i)) {
buf.append(lowStr.charAt(i));
} else {
buf.append(upStr.charAt(i));
}
return buf;
\u6269\u5c55\u8d44\u6599\uff1a
\u5b57\u7b26\u4e32\u5728\u5b58\u50a8\u4e0a\u7c7b\u4f3c\u5b57\u7b26\u6570\u7ec4\uff0c\u5b83\u6bcf\u4e00\u4f4d\u5355\u4e2a\u5143\u7d20\u90fd\u662f\u80fd\u63d0\u53d6\u7684\uff0c\u5b57\u7b26\u4e32\u7684\u96f6\u4f4d\u662f\u5b83\u7684\u957f\u5ea6\uff0c\u5982s[0]=10\uff0c\u8fd9\u63d0\u4f9b\u7ed9\u6211\u4eec\u5f88\u591a\u65b9\u4fbf\uff0c\u4f8b\u5982\u9ad8\u7cbe\u5ea6\u8fd0\u7b97\u65f6\u6bcf\u4e00\u4f4d\u90fd\u80fd\u8f6c\u5316\u4e3a\u6570\u5b57\u5b58\u5165\u6570\u7ec4\u3002
\u901a\u5e38\u4ee5\u4e32\u7684\u6574\u4f53\u4f5c\u4e3a\u64cd\u4f5c\u5bf9\u8c61\uff0c\u5982\uff1a\u5728\u4e32\u4e2d\u67e5\u627e\u67d0\u4e2a\u5b50\u4e32\u3001\u6c42\u53d6\u4e00\u4e2a\u5b50\u4e32\u3001\u5728\u4e32\u7684\u67d0\u4e2a\u4f4d\u7f6e\u4e0a\u63d2\u5165\u4e00\u4e2a\u5b50\u4e32\u4ee5\u53ca\u5220\u9664\u4e00\u4e2a\u5b50\u4e32\u7b49\u3002\u4e24\u4e2a\u5b57\u7b26\u4e32\u76f8\u7b49\u7684\u5145\u8981\u6761\u4ef6\u662f\uff1a\u957f\u5ea6\u76f8\u7b49\uff0c\u5e76\u4e14\u5404\u4e2a\u5bf9\u5e94\u4f4d\u7f6e\u4e0a\u7684\u5b57\u7b26\u90fd\u76f8\u7b49\u3002\u8bbep\u3001q\u662f\u4e24\u4e2a\u4e32\uff0c\u6c42q\u5728p\u4e2d\u9996\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\u7684\u8fd0\u7b97\u53eb\u505a\u6a21\u5f0f\u5339\u914d\u3002\u4e32\u7684\u4e24\u79cd\u6700\u57fa\u672c\u7684\u5b58\u50a8\u65b9\u5f0f\u662f\u987a\u5e8f\u5b58\u50a8\u65b9\u5f0f\u548c\u94fe\u63a5\u5b58\u50a8\u65b9\u5f0f\u3002
\u53c2\u8003\u8d44\u6599\u6765\u6e90\uff1a\u767e\u5ea6\u767e\u79d1-\u5b57\u7b26\u4e32
\u60a8\u597d\uff0c\u63d0\u95ee\u8005\uff1a
//\u7a0b\u5e8f\u6ca1\u6709\u8fd0\u884c\uff0c\u5982\u6709\u9519\u8bef\uff0c\u8bf7\u81ea\u884c\u4fee\u6539public class Print{public static void main(String[] args){ java.util.Scanner sc = new java.util.Scanner(System.in); String str = sc.next(); char[] arr = str.toCharArray(); int sum = 0;//\u7edf\u8ba1\u591a\u5c11\u4e2a\u5927\u5199\u5b57\u6bcd String result = ""; //\u8fde\u63a5\u5927\u5199\u5b57\u6bcd for(int i = 0; i = 65 && (int)arr[i] <= 90){ sum = ++sum; result += arr[i]; } } System.out.println("\u5927\u5199\u5b57\u6bcd\u4e00\u5171\uff1a" + sum+"---"); System.out.println("\u5206\u522b\u4e3a\uff1a" + result); } }}
{
//封装一个Map,key为String类型,value为字母数,其中key为UpperChar的value存大写字母数
//key为LowerChar的value存放小写字母数,key为OtherChar的value存放非英文字母数
Map<String,Integer> map = new HashMap<String,Integer>();
int upperValue = 0;
int lowerValue = 0;
int otherValue = 0;
for (int i = 0 ; i < str.length(); i++)
{
if (str.charAt[i] > 'a' && str.charAt[i] < 'z')
{
lowerValue++;
}
else if (str.charAt[i] > 'A' && str.charAt[i] < 'Z')
{
upperValue++;
}
else
{
otherValue++;
}
}
map.put("UpperChar",upperValue);
map.put("LowerChar",lowerValue);
map.put("OtherChar",otherValue);
return map;
}
然后再main()方法中调用这个函数,通过下面这种方式获取其值
Map<String,Integer> map = 对象名.getCharacterNum(inputStr);
int upperNum = (int) map.get("UpperChar");
public class UpcaseTest {
public int[] getNumber(String str) {
char[] c = str.toCharArray();
int[] number = new int[3];//数组中依次存放大写字母数、小写字母数和非英文数
for (int i = 0; i < c.length; i++) {
if (c[i] >= 'A' && c[i] <= 'Z') {
number[0]++;//大写字母数加1
}else if (c[i] >= 'a' && c[i] <= 'z') {
number[1]++;//小写字母数加1
}else{
number[2]++;//非英文数加1
}
}
return number;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "aaaASDFS.,/;'d";
int[] result=new UpcaseTest().getNumber(str);
for(int i=0;i<result.length;i++){
System.out.println(result[i]);
}
}
}
有不懂可以追问
public abstract class StringUtil{
String inputString = "";
//输入程序(初始化inputString)
//统计方法
public abstract long getLength();
public static void main(String[] args){
StringUtil stringUtil = new LowerUtil();
//调用输入程序
stringUtil.inputString = "Zhu Cheng feng is a good boy!";
System.out.println(stringUtil.getLength());
}
}
/**
* 处理字符串中的大写部分
* @author zcf
* @since 20140325
*
*/
class UpperUtil extends StringUtil{
@Override
public long getLength() {
if(!"".equals(inputString)){
return inputString.split("[A-Z]").length>0
?inputString.split("[A-Z]").length-1:0;
}
return 0;
}
}
/**
* 处理字符串中的小写部分
* @author zcf
* @since 20140325
*
*/
class LowerUtil extends StringUtil{
@Override
public long getLength() {
if(!"".equals(inputString)){
return inputString.split("[a-z]").length>0
?inputString.split("[a-z]").length-1:0;
}
return 0;
}
}
/**
* 处理字符串中的数值部分
* @author zcf
* @since 20140325
*
*/
/**
* 处理字符串中的符号部分
* @author zcf
* @since 20140325
*
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class OutputUpperCase
{
static String str = "AbCdE fGh NNMM ZZZ";
public static void main(String[] args)
{
Pattern upperPattern = Pattern.compile("[A-Z]");
Pattern lowerPattern = Pattern.compile("[a-z]");
int upperCount = getOutput(upperPattern);
int lowerCount = getOutput(lowerPattern);
Pattern nonLetterPattern = Pattern.compile("[^A-Za-z]");
Matcher matcher = nonLetterPattern.matcher(str);
int nonLetterCount = getOutput(matcher); // 调用了重载方法getOutput(Matcher)
System.out.println(upperCount + " Upper Chars");
System.out.println(lowerCount + " Lower Chars");
System.out.println(nonLetterCount + " Non Letter Chars");
}
private static int getOutput(Pattern pattern)
{
Matcher matcher = pattern.matcher(str);
int result = 0;
while(matcher.find())
{
result ++;
}
return result;
}
private static int getOutput(Matcher matcher)
{
int result = 0;
while(matcher.find())
{
result ++;
}
return result;
}
}
public class NumOfString
{
public static void main(String args[]) {
String s = "1jkhKhjklhklHLHjb90kBNo9" ;
int cU = 0 ;
int cL = 0 ;
int cN = 0 ;
char c ;
for(int i=0 ;i<s.length() ;i++) {
c = s.charAt(i) ;
if(c > 'a' && c < 'z') {
cL ++ ;
}
else if(c > 'A' && c < 'Z') {
cU ++ ;
}
else {
cN ++ ;
}
}
System.out.println("Number of LowerCase:" + cL) ;
System.out.println("Number of UpperCase:" + cU) ;
System.out.println("Number of NotChar:" + cN) ; }
绛旓細{ str = String.format("%04d", i);System.out.println(str);} } Java鏄竴闂ㄩ潰鍚戝璞缂栫▼璇█锛涓嶄粎鍚告敹浜咰++璇█鐨勫悇绉嶄紭鐐癸紝杩樻憭寮冧簡C++閲岄毦浠ョ悊瑙g殑澶氱户鎵裤佹寚閽堢瓑姒傚康锛屽洜姝Java璇█鍏锋湁鍔熻兘寮哄ぇ鍜岀畝鍗曟槗鐢ㄤ袱涓壒寰併侸ava璇█浣滀负闈欐侀潰鍚戝璞$紪绋嬭瑷鐨勪唬琛紝鏋佸ソ鍦板疄鐜颁簡闈㈠悜瀵硅薄鐞嗚锛屽厑璁绋嬪簭鍛...
绛旓細public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("杈撳叆a锛"); int a = scanner.nextInt(); System.out.print("杈撳叆b锛"); int b = scanner.nextInt(); System.out.print("杈撳叆c锛"); int c = scanner.n...
绛旓細鍦╩ain閲屽啓 String name;String age;Scanner sc=new Scanner(System.in);System.out.println("杈撳叆濮撳悕");name=sc.nextLine();System.out.println("杈撳叆骞撮緞");age=sc.nextLine();System.out.println("濮撳悕"+name);System.out.println("骞撮緞"+age);sc.close();...
绛旓細buffer.append("涓涓鍥炴枃涓层俓n");buffer.append("---\n");System.out.println(buffer.toString());} else { System.out.println("閫鍑绋嬪簭銆");} } } } 涓嬮潰鏄祴璇曠殑鏁版嵁锛--- 杈撳叆涓涓暟瀛楋細123321 鎮ㄨ緭鍏ョ殑鏄細123321 鍏 6 涓瓧绗︺123321 鏄叏鏁板瓧銆123321 鏄竴涓6浣嶇殑鏁存暟銆123321...
绛旓細import java.util.Scanner;/ 鐢ㄦ埛杈撳叆n1鍜宯2锛氭眰涓嶈兘鍚屾椂琚3鍜5鏁撮櫎鐨勭疮鍔犲拰 author 浣犲ソqiulinhe / public class Ceshi { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);System.out.println("璇蜂緷娆¤緭鍏1-10鐨勬暟瀛");for(int i=1;i<11;i++){ int...
绛旓細浠ヤ笅鏄涓涓娇鐢↗ava缂栧啓鐨绋嬪簭锛鐢ㄤ簬姹傚嚭涓涓暣鏁扮殑鎵鏈夊洜瀛愮殑鍜岋細鍦ㄨ繖涓▼搴忎腑锛岄鍏堜粠鐢ㄦ埛閭i噷鑾峰彇涓涓暣鏁拌緭鍏ャ傜劧鍚庯紝浣跨敤涓涓惊鐜潵閬嶅巻浠1鍒拌鏁存暟鐨勬墍鏈夋暣鏁般傚浜庢瘡涓暣鏁帮紝濡傛灉瀹冩槸璇ユ暣鏁扮殑鍥犲瓙锛堝嵆璇ユ暣鏁伴櫎浠ヨ鏁存暟鐨勪綑鏁颁负0锛夛紝鍒欏皢璇ユ暣鏁板姞鍒版诲拰涓傛渶鍚庯紝杈撳嚭鎵鏈夊洜瀛愮殑鍜屻備緥濡傦紝濡傛灉鐢ㄦ埛...
绛旓細import javax.swing.JOptionPane;public class X65 { public static void main(String []args){ double[]nums=new double[10];String output="涓嶅悓鐨勬暟鏄";for(int i=0;i<=9;i++) { String a=JOptionPane.showInputDialog("杈撳叆鍗佷釜鏁");nums[i]=Double.parseDouble(a);int k;k=1;for...
绛旓細鎸夌収浣犵殑瑕佹眰缂栧啓鐨Java绋嬪簭濡備笅 import java.util.Scanner;public class A { public static void main(String[] args) { System.out.println("璇疯緭鍏ヤ换鎰涓涓瀛楃涓:");Scanner sc=new Scanner(System.in);String s=sc.nextLine();System.out.println(s+"鐨勯暱搴︿负"+s.length());System.out....
绛旓細import java.io.IOException;import java.io.BufferedReader;import java.io.InputStreamReader;class Round{ public double diameter(double d){ return d*2;} public double perimeter(double d){ return 2*d*3.14;} public double area(double d){ return d*d*3.14;} } public class ...
绛旓細閫氳繃閿洏杈撳叆涓や釜鏁存暟锛屾眰涓ゆ暟鐨勫拰鐨凧ava绋嬪簭濡備笅鍥炬墍绀猴細缂栧啓Java绋嬪簭鏃讹紝搴旀敞鎰忎互涓嬪嚑鐐癸細澶у皬鍐欐晱鎰燂細Java鏄ぇ灏忓啓鏁忔劅鐨勶紝杩欏氨鎰忓懗鐫鏍囪瘑绗ello涓巋ello鏄笉鍚岀殑銆傜被鍚嶏細瀵逛簬鎵鏈夌殑绫绘潵璇达紝绫诲悕鐨勯瀛楁瘝搴旇澶у啓銆傚鏋滅被鍚嶇敱鑻ュ共鍗曡瘝缁勬垚锛岄偅涔堟瘡涓崟璇嶇殑棣栧瓧姣嶅簲璇ュぇ鍐欙紝渚嬪 MyFirstJavaClass銆傛柟娉...