【发布时间】:2016-11-17 19:54:45
【问题描述】:
我有一个关于如何计算数组中字母数量的作业。 这是我的代码:
import java.util.Scanner;
public class task1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Create a scanner object
Scanner input = new Scanner(System.in);
//Prompt user's input
System.out.println("Enter strings (use a space to separate them; hit enter to finish) : ");
String str = input.nextLine();
// use split to divide the string into different words cutting by " "
String [] wordsArray = str.trim().split(" ");
System.out.println("The length of string is " + wordsArray.length);
for(int i = 0 ; i < wordsArray.length; i++){
char [] eachLetterinArray = wordsArray[i].toCharArray();
for(int j = 0,count = 0 ; j < eachLetterinArray.length; j++){
if( (eachLetterinArray[j]+'a'-97 >=65 && eachLetterinArray[j]+'a'-97 <=90 )
|| (eachLetterinArray[j]+'a'-97 >=97 && eachLetterinArray[j]+'a'-97 <=122 ) ){
count++;
}
System.out.print(count);
}
}
}
如果我输入“end tr” 输出为“12312” 但我想要的是“3和2”
我已经尝试了很多,但仍然无事可做...... 你能帮帮我吗?
【问题讨论】:
-
// TODO Auto-generated method stub似乎为您生成了 IDE。在 IDE 中尝试调试器 -
但是我是java新手..我不知道如何在IDE中尝试调试器..
-
您正在循环内打印。把
System.out.print(count);下面两行就行了
标签: java