我目前做出..
public class aa
{
public static void main(String[]args)throws java.io.IOException
{
String s1=new String();
int b,l=0,s=0;
int bigchar=0;
int number=0;
int aa=0;
int smallchar=0;
System.out.print("enter anything:");
s1=KBInput.readString();
System.out.println("you type in:"+s1);
System.out.println(s1.length());
if(Character.isUpperCase(s1))
bigchar++;
else if(Character.isLowerCase(s1))
smallchar++;
else if(Character.isDigit(s1))
number++;
else
aa++;
}
}
目前還沒完成...我不知道怎麼接下去了= =...
請問我要怎麼改才能改成
當我輸入一串英文時他會自動count 有多少個英文字和多少個空白..
大寫小寫和有多少個a在這組字串呢??
2007-05-10 03:13:05 · 2 個解答 · 發問者 暗影之心 1 in 電腦與網際網路 ➔ 程式設計
請參考我的做法
public class aa {
public static void main(String[]args)throws java.io.IOException {
String s1=new String();
int b,l=0,s=0;
int bigchar=0; // uppercase characters
int number=0; // digits
int spaces= 0; // spaces
int aa=0; // character 'a'
int smallchar=0; // lowercase characters
int chr = 0; // English characters
System.out.print("enter anything:");
s1=KBInput.readString();
System.out.println("you type in:"+s1);
System.out.println(s1.length());
for (int i = 0; i < s1.length(); i++) {
char c = s1.charAt(i);
if (Character.isUpperCase(c)) bigchar++;
if (Character.isLowerCase(c)) smallchar++;
if (Character.isDigit(c)) number++;
if (Character.isWhitespace(c)) spaces++;
if (c == 'a') aa++;
if (c >= 'A' && c <= 'Z' ||
c >= 'a' && c <= 'z') chr++;
}
System.out.println("English characters: " + chr);
System.out.println("White spaces: " + spaces);
System.out.println("Uppercase: " + bigchar);
System.out.println("Lowercase: " + smallchar);
System.out.println("Character 'a': " + aa);
}
}
2007-05-10 06:37:17 · answer #1 · answered by ? 7 · 0⤊ 0⤋
//Power by Eclipse v3.2
import java.util.*;
import java.io.*;
//測試類別檔名 TEST_JAVA.java
public class TEST_JAVA
{
public static void main(String[] args)
{
//==========START==========//
Scanner input=new Scanner(System.in);
PrintStream p=new PrintStream(System.out);
p.printf("Input a String: ");
String str=input.nextLine();
p.printf("The Length of String: %d\n", str.length());
p.printf("The Number of Space: %d\n", COUNT(str, 32));
p.printf("The Number of Letter: %d\n", str.length()-COUNT(str, 32));
p.printf("The Number of A: %d\n", COUNT(str, 65));
p.printf("The Number of a: %d\n", COUNT(str, 97));
//…以此類推
//==========END==========//
}
public static int COUNT(String str, int ch){
int COUNTER=0;
for(int i=0;i
i=str.indexOf(ch,i);
if(i==-1)
break;
COUNTER++;
}
return COUNTER;
}
}
2007-05-10 05:49:50 · answer #2 · answered by Big_John-tw 7 · 0⤊ 0⤋