2 sinat 36064115 sinat_36064115 于 2016.09.07 20:08 提问

Java问题求解 急!拜托帮忙

scanner输入一句话 输出单词去掉标点换行

输入
A: May I ask who is calling, please?
输出
A
May
I
ask
who
is
calling
please

2个回答

jiang_xiansen
jiang_xiansen   2016.09.07 20:41

Scanner scanner = new Scanner(System.in);
if(scanner.hasNext()){
String ss = scanner.nextLine().split("\W+");
for(String s: ss) System.out.println(s);
}
希望帮到你。

sinat_36064115
sinat_36064115 这里didCR eclipse显示错误 然后接下来怎么写呢?
一年多之前 回复
sinat_36064115
sinat_36064115 else { if ( didCR == false ) { // output CR if previous output was not a CR System.out.println(); didCR = true; // previous output was a CR }
一年多之前 回复
sinat_36064115
sinat_36064115 Scanner s = new Scanner(System.in); String line = s.nextLine(); System.out.println("请输入一句话"+line); int i; char ch; for ( i = 0; i < line.length(); i++ ) { ch = line.charAt(i); if ( Character.isLetter(ch) ) { System.out.println(ch); didCR = false; // previous output was not a CR
一年多之前 回复
sinat_36064115
sinat_36064115 请问有没有详细的办法?刚学不太懂
一年多之前 回复
bigwhitetao9527
bigwhitetao9527   2016.09.07 23:12
 String str = "A: May I ask who is calling, please? ";
        StringBuffer strb = new StringBuffer(str);


        //剔除字符串中的符号
        for(int i=0;i<strb.length();i++){
            if((strb.charAt(i)>='A' && strb.charAt(i)<='Z')||(strb.charAt(i)>='a' &&strb.charAt(i)<='z')||strb.charAt(i)==' '){
                continue;
            }
            else{
                strb.deleteCharAt(i);
            }
        }

        //分行输出
        str = strb.toString();
        String[] strs = str.split(" ");

        for(int i=0;i<strs.length;i++){
            System.out.println(strs[i]);
        }

临时写的,看下能不能看懂~

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!