關於java輸入字串並且會記錄..

2007-05-10 3:13 pm
我目前做出..

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在這組字串呢??

回答 (2)

2007-05-10 6:37 pm
✔ 最佳答案
請參考我的做法

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 5:49 pm
//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<str.length();i++)
{
i=str.indexOf(ch,i);
if(i==-1)
break;
COUNTER++;
}
return COUNTER;
}
}


收錄日期: 2021-04-27 17:16:22
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070510000015KK01645

檢視 Wayback Machine 備份