从键盘输入一串字符串,编写一个java程序实现统计,输出有几个大写字母,几个小写字母,和几个数字? 编写一个程序,从键盘上输入一串字符串,分别统计字符串中大写字...

\u4ece\u952e\u76d8\u8f93\u5165\u4e00\u4e32\u5b57\u7b26\u4e32\uff0c\u7f16\u5199\u4e00\u4e2ajava\u7a0b\u5e8f\u5b9e\u73b0\u7edf\u8ba1\uff0c\u8f93\u51fa\u6709\u51e0\u4e2a\u5927\u5199\u5b57\u6bcd\uff0c\u51e0\u4e2a\u5c0f\u5199\u5b57\u6bcd\uff0c\u548c\u51e0\u4e2a\u6570\u5b57,\u5e76\u628a\u5927

\u8fd9\u4e2a\u662f\u5904\u7406\u6587\u4ef6\u7684\u7c7bFileUtil\uff1a
=================================================
package org.xhome.leon.test;
import java.io.*;
public class FileUtil {
FileReader fr;
BufferedReader br;
FileWriter fw;
BufferedWriter bw;
String source = "";

public int upCaseNum = 0;
public int lowerCaseNum = 0;
public int numerNum = 0;
public int otherNum = 0;

public void initReaders(String fileName) throws FileNotFoundException {

fr = new FileReader(fileName);
br = new BufferedReader(fr);
}
public void initWriters(String fileName) throws IOException {

fw = new FileWriter(fileName);

bw = new BufferedWriter(fw);
}
public void readFile(String fileName) throws FileNotFoundException {
this.initReaders(fileName);
this.source = "";
upCaseNum = 0;
lowerCaseNum = 0;
numerNum = 0;
otherNum = 0;
String sr;
try {
while ((sr = br.readLine()) != null) {
source = source.concat(sr);
}
if (source != null) {
char[] myChars = source.trim().toCharArray();
for (int i = 0; i < myChars.length; i++) {
if (myChars[i] >= 'a' && myChars[i] <= 'z') {
lowerCaseNum++;
} else if (myChars[i] >= 'A' && myChars[i] <= 'Z') {
upCaseNum++;
} else if (myChars[i] >= '0' && myChars[i] <= '9') {
numerNum++;
} else {
otherNum++;
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
this.closeReaderIO();
}
}
public void writeFile(String infileName,String outfileName, boolean isToUperCase) throws IOException {
this.initWriters(outfileName);
this.initReaders(infileName);
String st;
try {
if (isToUperCase) {
while ((st = br.readLine()) != null) {
st = st.toUpperCase();
try {
bw.write(st);
bw.newLine();
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
while ((st = br.readLine()) != null) {
st = st.toLowerCase();
try {
bw.write(st);
bw.newLine();
bw.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
this.closeReaderIO();
this.closeWriterIO();
}
}
public void closeReaderIO() {
if (this.fr != null) {
try {
this.fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (this.br != null) {
try {
this.br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void closeWriterIO() {
if (this.fw != null) {
try {
this.fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (this.bw != null) {
try {
this.bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
\u4e0b\u9762\u662f\u754c\u9762\u7f16\u5199\u7684\u7c7b\uff1a
=========================================================
package org.xhome.leon.view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.*;
import org.xhome.leon.test.FileUtil;
public class MainView extends JFrame{
JPanel upPanel = new JPanel();
JPanel downPanel = new JPanel();
JLabel rlabel = new JLabel("\u8bfb\u5165\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84");
JLabel wlabel = new JLabel("\u5199\u51fa\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84");

JTextField rjtf = new JTextField();
JTextField wjtf = new JTextField();
JButton rbtn = new JButton("\u8bfb\u53d6");
JButton wbtn = new JButton("\u5199\u5165");
JButton upORlow = new JButton("\u5c0f\u5199\u65b9\u5f0f\u5199\u5165");

JSplitPane jsp;
JTextArea jta = new JTextArea();
boolean isUperCase = false;

FileUtil ft;

public MainView(){

ft = new FileUtil();
this.setBounds(200, 100, 600, 400);
this.init();


this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JOptionPane.showMessageDialog(this, "\u8bf7\u6ce8\u610f\uff0c\u6587\u4ef6\u8def\u5f84\u5206\u9694\u7b26\u4e3a / ,\u800c\u975e \\,\u6bd4\u5982D\u76d8\u4e0b\u7684test.txt\n\u4e3aD:/test.txt");
}

public void init(){
upPanel.setLayout(null);
rlabel.setBounds(5, 5, 150, 30);
wlabel.setBounds(5, 80, 150, 30);
rjtf.setBounds(165, 5, 215, 30);
wjtf.setBounds(165, 80, 215, 30);
rbtn.setBounds(505, 5, 60, 30);
wbtn.setBounds(505, 80, 60, 30);
upORlow.setBounds(5, 120, 120, 30);

rbtn.addActionListener(new MyButtonListener());
wbtn.addActionListener(new MyButtonListener());
upORlow.addActionListener(new MyButtonListener());

upPanel.add(rlabel);
upPanel.add(wlabel);
upPanel.add(rjtf);
upPanel.add(wjtf);
upPanel.add(rbtn);
upPanel.add(wbtn);
upPanel.add(upORlow);

jta.setEditable(false);
downPanel.add(jta);

jsp = new JSplitPane(JSplitPane.VERTICAL_SPLIT, upPanel, downPanel);
jsp.setDividerLocation(170);
jsp.setEnabled(false);
this.add(jsp);
}
class MyButtonListener implements ActionListener{

public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(rbtn)){
try {
ft.readFile(rjtf.getText().toString().trim());
jta.setText("\u5927\u5199\u5b57\u6bcd\uff1a"+ft.upCaseNum+"\n\n");
jta.append("\u5c0f\u5199\u5b57\u6bcd\uff1a"+ft.lowerCaseNum+"\n\n");
jta.append("\u6570\u5b57\uff1a"+ft.numerNum+"\n\n");
jta.append("\u5176\u4ed6\uff1a"+ft.otherNum+"\n\n");

} catch (FileNotFoundException e1) {

JOptionPane.showMessageDialog(MainView.this, "\u8bfb\u53d6\u9519\u8bef\uff0c\u8bf7\u786e\u8ba4\u6587\u4ef6\u8def\u5f84\u662f\u5426\u6b63\u786e");
return;
}



}
else if(e.getSource().equals(wbtn)){
String inFile = rjtf.getText().toString().trim();
if(inFile != null && !inFile.equals("")){
try {
ft.writeFile(inFile, wjtf.getText().toString().trim(), isUperCase);
JOptionPane.showMessageDialog(MainView.this, "\u5199\u5165\u6210\u529f\uff01");
} catch (IOException e1) {
JOptionPane.showMessageDialog(MainView.this, "\u5199\u5165\u9519\u8bef\uff0c\u8bf7\u786e\u8ba4\u6587\u4ef6\u8def\u5f84\u662f\u5426\u6b63\u786e");
return;
}
}
}
else if(e.getSource().equals(upORlow)){
if(isUperCase){
isUperCase = false;
upORlow.setText("\u5c0f\u5199\u65b9\u5f0f\u5199\u5165");
}
else{
isUperCase = true;
upORlow.setText("\u5927\u5199\u65b9\u5f0f\u5199\u5165");
}
}

}

}
}
===============================================
\u4e0b\u9762\u662f\u4e00\u4e2a\u88c5main\u65b9\u6cd5\u7684\u7c7b\uff1a
package org.xhome.leon.main;
import org.xhome.leon.view.MainView;
public class Launcher {
public static void main(String[] args) {
new MainView();
}
}
======================================
\u4e0b\u9762\u662f\u6211\u7528\u81ea\u5df1\u5199\u7684\u90a3\u4e2ajava\u6587\u4ef6\u6d4b\u8bd5\u7684\u7ed3\u679c\u622a\u56fe\uff1a
=======================================
ps\uff1a\u8fd9\u4e2a\u662f\u770b\u4e86\u697c\u4e3b\u7684\u8981\u6c42\u540e\u5199\u7684\u3002\u7528\u7684\u662f\u7a97\u53e3\u7a0b\u5e8f\u5b9e\u73b0\uff0c\u5982\u679c\u4e0d\u7528\u754c\u9762\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u7528FileUtil\u8fd9\u4e2a\u7c7b\u8fdb\u884c\u64cd\u4f5c\u5c31\u53ef\u4ee5\u4e86\u3002 \u5e0c\u671b\u5bf9\u4f60\u7528\u5e2e\u52a9 ^ ^

java \u8bed\u8a00\u53ef\u4ee5\u4e48\uff1f
import java.util.*;
public class charc {
public static void main(String[] args) {
int letter=0,digit=0,space=0;
Scanner sc=new Scanner(System.in);
System.out.println("\u8bf7\u8f93\u5165\u4e00\u884c\u5b57\u7b26");
String str=sc.nextLine();
char[] ch=str.toCharArray();
for(int i=0;i<ch.length;i++){
if(Character.isDigit(ch[i])){
digit++;
}
if(Character.isLetter(ch[i])){
letter++;
}
if(Character.isSpaceChar(ch[i])){
space++;
}
}
System.out.println("\u8f93\u51fa\u6709\u6709"+digit+"\u4e2a\u6570\u5b57");
System.out.println("\u8f93\u51fa\u6709\u6709"+letter+"\u4e2a\u5b57\u6bcd");
System.out.println("\u8f93\u51fa\u6709\u6709"+space+"\u4e2a\u7a7a\u683c");

}

}
\u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a
\u8bf7\u8f93\u5165\u4e00\u884c\u5b57\u7b26
qre 123 r
\u8f93\u51fa\u6709\u67093\u4e2a\u6570\u5b57
\u8f93\u51fa\u6709\u67094\u4e2a\u5b57\u6bcd
\u8f93\u51fa\u6709\u67092\u4e2a\u7a7a\u683c

import java.io.*;import java.util.*;
public class Static{private String input;
private int bLetter =0;private int sLetter =0;private int numbers =0;private int others =0;private int spaces =0;
public Static(){   this.getString();   char[] list = charsOfString(input);
   for (int i=0;i<list.length ;i++ )   {    int flag = letterToCode(list[i]);
    if(flag>=65 && flag<=90) //注意:这里是大写字母的ASCII码值范围,下面一样    {     bLetter += 1 ;    }    else if (flag>=97 && flag<=122)    {     sLetter += 1;    }    else if (flag>=48 && flag<=57)    {     numbers += 1;    }    else if (flag == 32)    {     spaces += 1;    }    else    {     others += 1;    }   }
   System.out.println("您输入的字符串中的大写字母有:"+bLetter);   System.out.println("您输入的字符串中的小写字母有:"+sLetter);   System.out.println("您输入的字符串中的数字有:"+numbers);   System.out.println("您输入的字符串中的空格有:"+spaces);   System.out.println("您输入的字符串中的其他字符有:"+others);
}
public static void main(String args[]){   new Static();}
/*** 该方法获取一组字符串*/public void getString(){   BufferedReader in = new BufferedReader(                   new InputStreamReader(System.in));   System.out.println("请输入一串字符串:");
   try   {    this.input = in.readLine();   }   catch (Exception e)   {    System.out.println(e);   }}    /*** 该方法返货该字符串的各个字符到一个数组里* 这里我打算用ArrayList的,但是他只能保存为Object类型* 就是不知道怎么把Object转换为char,所以,这里就用char[]*/public char[] charsOfString(String s){   char[] list = new char[s.length()];
   for (int i=0 ;i<s.length() ;i++ )   {    list[i] = s.charAt(i);   }
   return list;}    /*** 该方法获将字母转换为对应ASCII码*/public int letterToCode(char ch){   int s = ch;   return s; }}

你试试看 我也参考别人的



  • C璇█缂栫▼棰 4.缂栫▼瀹炵幇:鐢鐢ㄦ埛浠庨敭鐩樿緭鍏ヤ竴涓插瓧绗(浠ュ洖杞﹂敭缁撴潫),缁 ...
    绛旓細include<stdio.h>#include<stdlib.h>int main(){ char a[128]; gets(a); int i=0; int c1,c2,c3,c4; c1=c2=c3=c4=0; while(a[i++]) { if(a[i]>='A'&& a[i]<='Z') c1++; else if(a[i]>='a'&& a[i]<='z') c2++; else if...
  • c璇█,浠庨敭鐩樿緭鍏ヤ竴涓插瓧绗︿覆,缁熻瀛楃涓蹭腑鐗瑰畾瀛楃鐨勪釜鏁,骞惰緭鍑...
    绛旓細绋嬪簭璁捐鎬濊矾濡備笅锛浠庨敭鐩鍒嗗埆杈撳叆瀛楃涓插拰瑕佺粺璁$殑瀛楃锛鐒跺悗瀵规瀛楃涓蹭粠澶村紑濮嬮愪釜涓庢墍缁熻鐨勫瓧绗︽瘮杈冿紝濡傜浉鍚岋紝鍒欒璁℃暟鍣ㄥ姞1锛岀煡閬撳瓧绗︿覆鏁翠綋姣旇緝缁撴潫涓烘锛岃鏁板櫒涓氨鏄渶缁熻鐨勫瓧绗︾殑涓暟銆俰nclude "stdio.h"main(){ char str[100],ch; /*瀹氫箟str瀛楃涓诧紝瀹氫箟ch鐢ㄦ潵瀛樻斁瑕佺粺璁$殑瀛楃*/ ...
  • 浠庨敭鐩樿緭鍏ヤ竴涓浠绘剰瀛楃(鍙寘鍚┖鏍),缂栧啓绋嬪簭浠庝腑鎵惧埌鏈闀垮瓧姣嶅瓧涓...
    绛旓細/*** 璇ユ柟娉曡幏鍙栦竴缁勫瓧绗︿覆*/public void getString(){ BufferedReader in = new BufferedReader( new InputStreamReader(System.in)); System.out.println("璇杈撳叆涓涓插瓧绗︿覆:");try { this.input = in.readLine(); } catch (Exception e) { System.out.println(...
  • 缂栧啓绋嬪簭,缁熻浠庨敭鐩樿緭鍏ヤ竴涓浠'#'缁撴潫鐨瀛楃涓,缁熻瀛楃涓茬殑瀛楃涓暟...
    绛旓細include<stdio.h>main(){ char s[1000]; int i; while(1){ i=0; scanf("%c",&s[0]); while(s[i]!='#'){ i++; scanf("%c",&s[i]);}getchar();printf("%d涓猏n",i);}}濡傚浘鎵绀猴紝绠楁椂绌烘牸绗︼紝鎹㈣绗﹂兘绠楅噷鐨勶紝鏈涢噰绾炽傘傘
  • 绋嬪簭璁捐棰樼洰:浠庨敭鐩涓杈撳叆涓涓瓧绗︿覆,鏀惧湪鏁扮粍涓,瑕佹眰灏嗗瓧绗︿覆涓殑...
    绛旓細涓嬮潰鏄竴涓 Java 绋嬪簭锛屽畠鍙互浠庨敭鐩涓婅鍙涓涓瓧绗︿覆锛骞跺皢鍏朵腑鐨勫ぇ鍐欏瓧姣嶈浆鎹负灏忓啓瀛楁瘝锛屽悓鏃跺皢灏忓啓瀛楁瘝杞崲涓哄ぇ鍐欏瓧姣嶃傜▼搴忕殑鍩烘湰鎬濊矾濡備笅锛1. 浣跨敤 `Scanner` 绫讳粠鏍囧噯杈撳叆涓鍙栦竴涓瓧绗︿覆锛屽瓨鍌ㄥ湪涓涓 `String` 绫诲瀷鐨勫彉閲忎腑銆2. 灏嗚瀛楃涓茶浆鎹负瀛楃鏁扮粍锛屼究浜庨愪釜璁块棶姣忎釜瀛楃骞惰繘琛岃浆鎹...
  • 浠庨敭鐩樿緭鍏ヤ竴涓插瓧绗︿覆,缁熻瀛楃涓蹭腑鐗瑰畾瀛楃鐨勪釜鏁(鐗瑰畾瀛楃闇瑕佷粠閿洏...
    绛旓細鎬濊矾锛浠庨敭鐩鍒嗗埆杈撳叆瀛楃涓插拰瑕佺粺璁$殑瀛楃锛鐒跺悗瀵规瀛楃涓蹭粠澶村紑濮嬮愪釜涓庢墍缁熻鐨勫瓧绗︽瘮杈冦傚鐩稿悓锛屽垯璁╄鏁板櫒鍔1锛岀煡閬撳瓧绗︿覆鏁翠綋姣旇緝缁撴潫涓烘锛岃鏁板櫒涓氨鏄渶缁熻鐨勫瓧绗︾殑涓暟锛屽叿浣撲唬鐮佽璁″涓嬶細鍑芥暟搴旂敤 1銆佽繛鎺ヨ繍绠 concat(s1,s2,s3鈥n) 鐩稿綋浜巗1+s2+s3+鈥+sn.渚嬶細concat(鈥11鈥,'aa鈥...
  • 浠庨敭鐩樿緭鍏ヤ竴涓插瓧绗︿覆,鎵惧埌鍏朵腑鐨勬暟瀛楀瓧绗﹀苟杈撳嚭
    绛旓細include "stdio.h"main(){ char a[100];int i;gets (a);for(i=0;a[i]!='\0';i++)if(a[i]>='0'&&a[i]<='9')printf("%c",a[i]);}
  • 浠庨敭鐩樿緭鍏ヤ竴涓鏈夊瓧姣嶅拰鏁板瓧娣峰悎鐨瀛楃涓,缂栧啓绋嬪簭灏嗗瓧绗︿覆涓殑瀛愭瘝鍜...
    绛旓細include<stdio.h> main(){ char c;FILE *fp1 =fopen("c:\\char.txt","wt"); /*鍙啓寤虹珛涓涓鏂囨湰鏂囦欢*/ FILE *fp2 =fopen("c:\\number.txt","wt");while((c=getch())!='\r'){ putch(c); /*灞忓箷杈撳嚭鎵鏈瀛楃*/ if(c>='0'&&c<='9')fputc(c,fp2); /*杈撳嚭鏁板瓧...
  • 璇缂栧啓涓涓绋嬪簭,浠庨敭鐩樿緭鍏ュ瓧绗︿覆,缁熻瀛楃涓蹭腑灏忓啓瀛楁瘝a鍒癴鍚勮嚜鍑虹幇...
    绛旓細?璇█瀹炵幇 include <stdio.h> include <string.h> main(){ char str[128];int pp[6];int i,L;printf("Please input;");scanf("%s",str);L=strlen(str);for(i=0;i<L;i++) if(str[i]>='a'&&str[i]>='f') pp[str[i]-'a']++;} === include <stdio.h> main(){ cha...
  • 璇锋暀,C璇█瀹炵幇銆浠庨敭鐩涓杈撳叆涓涓瓧绗︿覆,缂栧啓涓涓绋嬪簭瀹屾垚鎸囧畾瀛楁瘝鐨...
    绛旓細浣犲ソ锛屽緢楂樺叴涓轰綘瑙g瓟锛屼唬鐮佸涓嬶細include <stdio.h> include <string.h> main(){ char a[100];gets(a);//鏁磋杈撳叆 int len = strlen(a);//姹瀛楃涓闀垮害 for(int i = 0;i < len;i++)//閫愪竴妫楠 { if(a[i]== 'i')printf("a");else printf("%c",a[i]);} printf("\n...
  • 扩展阅读:一键生成代码免费 ... 找出字符串中的数字并输出 ... 电脑键盘特殊符号一览表 ... 编写一个程序将两个字符串连接 ... 手机字符大全 ... 8个字符的密码例子100个 ... 键盘打出特殊符号大全 ... 输入一串字符反向输出 ... 输入一串字符串只输出数字 ...

    本站交流只代表网友个人观点,与本站立场无关
    欢迎反馈与建议,请联系电邮
    2024© 车视网