一个小程序,思前想后想不出,也许是我也不再太熟有个字符串How are you! ,编写程序,让它输出 !you are H
一个小程序,思前想后想不出,也许是我也不再太熟 有个字符串"How are you! ",编写程序,让它输出" !you are How"(注意有空格) 我主要求思路 看看怎么遇见一个编程题怎么考虑[最优解释]
引用: Java code?123String str="how are you!"; StringBuffer buffer=new StringBuffer(str); System.out.println(buffer.reverse()); 你没有看清题
[其他解释] How are you!
1. 先整个字符串逆转
!uoy era woH
2. 在遇到非字符,如空格,把两个非字符之间的部分逆转
!you are How
[其他解释] 引用: Java code?123456789101112131415161718192021222324252627282930313233343536373839package com.zr.csdn; import java.util.regex.Matcher;import java.util.regex.Pattern; public class StringReve…… 不错不错。。。学到点正则
[其他解释] 引用: 引用:写个正则提取单词与标点,每个单词或者标点作为一个String,之后添加到Collections(比如List)调用Collections的reverse翻转就行里Java code?1234567891011121314151617181920212223242526272829303132333435363738394041424…… 一次笔试出的 我也郁闷
[其他解释] 代码写少了,方法见得少了,肯定没有思路了!
[其他解释] 改进了一下,这个应该没问题了。当然也存在一些地方需要改善,比如存在两个!什么的,这里懒一下,就不做了
package com.briup.test08; public class TestYield2 { public static void main(String[] args) { String str ="How are you!"; String[] str1 = str.split(" "); String out = ""; for (int i = str1.length-1; i >=0; i--) { if(str1[i].contains("!")){ //System.out.println(str1[i]); str1[i]="!"+str1[i]; //System.out.println(str1[i]); str1[i]=str1[i].substring(0, str1[i].length()-1); //System.out.println(str1[i]); } out+=str1[i]+" "; } System.out.println(out); } }[其他解释] 该回复于2012-11-23 08:43:55被管理员删除
[其他解释] (1)split分段
(2)用正则表达式提取单词和标点,并存入数组
(3)倒着输出数组
[其他解释] 。。。晕,你不是说思路么。
import java.util.regex.Matcher; import java.util.regex.Pattern; public class StringReverse { public static void main(String[] args) { String str ="How are you!"; String str1 = ""; Pattern p = Pattern.compile("([a-zA-Z]+)[其他解释] 引用: 写个正则提取单词与标点,每个单词或者标点作为一个String,之后添加到Collections(比如List)调用Collections的reverse翻转就行里 import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReverseWords { public static void main(String[] args){ ReverseWords test=new ReverseWords(); String[] result=test.reverse(test.spilt("how are you! how are you!")); for(int i=0;i<result.length;i++){ if(!result[i].matches("\\p{Punct}")) System.out.print(" "+result[i]); } //Output: !you are how !you are how } /** * 切分各个单词与标点 * @param input 待切分的句子 * @return */ public String[] spilt(String input){ Matcher m=Pattern.compile("([\\p{Punct}]+[其他解释] ([^\\w+ ])"); Matcher m = p.matcher(str); while(m.find()){ int start = m.start(); int end = m.end(); str1 = str1 + str.substring(start,end) + " "; } String[] str2 = str1.split(" "); for (int i = str2.length-1; i >=0; i--){ System.out.print(str2[i] + " "); } System.out.println(); System.out.println("**************************"); for (int i = str2.length-1; i >=0; i--){ Pattern p1 = Pattern.compile("[^\\w+]"); Matcher m1 = p1.matcher(str2[i]); if(m1.find()){ System.out.print(" " + str2[i]); } else{ System.out.print(str2[i] + " "); } } } }这个只对单符合适用,多符号可以稍微再改改
[其他解释] 引用: Java code?123456789101112131415161718192021222324252627282930313233343536373839package com.zr.csdn; import java.util.regex.Matcher;import java.util.regex.Pattern; public class StringReve…… 强大 谢谢
可惜现在的我还是无法理解的(努力学习ING)
[其他解释] 引用: 太多方法啦~举两个例子 Method 1: public static void reverse(String str) { char a[]=str.toCharArray(); for(int i=a.length-1;i>=0;i--) { System.out.print(a[i]); } } Method 2: public stat……
同学 不是倒序
[其他解释] 用一个字符串数组,读取每个单词,然后倒着输出就行了
[其他解释] 楼上正解~~~~
[其他解释] 太多方法啦~举两个例子
Method 1:
public static void reverse(String str)
{
char a[]=str.toCharArray();
for(int i=a.length-1;i>=0;i--)
{
System.out.print(a[i]);
}
}
Method 2:
public static void reverse(String str)
{
StringBuffer sb=new StringBuffer(str);
System.out.print(sb.reverse().toString());
}
[其他解释] String str ="How are you !";
String[] str1 = str.split(" ");
String out = "";
for (int i = str1.length-1; i >=0; i--)
{
out+=str1[i]+" ";
}
System.out.println(out);
[其他解释] 请注意输出 " !you are How" !前面有个空格 我主要还是求思路
[其他解释] 引用: 用一个字符串数组,读取每个单词,然后倒着输出就行了 请看清题目,真崩溃
[其他解释] 引用: String str ="How are you !"; String[] str1 = str.split(" "); String out = ""; for (int i = str1.length-1; i >=0; i--) { out+=str1[i]+" "; } …… 程序也有点小bug
你的结果是下面的:
you! are How
[其他解释] 引用: 改进了一下,这个应该没问题了。当然也存在一些地方需要改善,比如存在两个!什么的,这里懒一下,就不做了 Objective C code?1234567891011121314151617181920212223package com.briup.test08;public class TestYield2 { public static void m…… 你的结果:
!you
!you
!you
[其他解释] 引用: (1)split分段 (2)用正则表达式提取单词和标点,并存入数组 (3)倒着输出数组 大哥 能给个例子吗??
[其他解释] 我就不说StringBuffer有个reverse方法
[其他解释] 引用: 引用: 改进了一下,这个应该没问题了。当然也存在一些地方需要改善,比如存在两个!什么的,这里懒一下,就不做了 Objective C code?1234567891011121314151617181920212223package com.briup.test08;public class TestYield2 { public stat……
你确定你代码复制的没问题?
你仔细看看,我这里测试是没有问题的。
直接从这上面剪贴过去是需要更改换行的,
换行的时候你肯定给多删除了某些地方。
我的某些测试注释了之后你不会又给填上了把?
“ !you are How"和“!you are How”差别不大吧?就是改个前后语句顺序
[其他解释] ([^\\w+ ])");
Matcher m = p.matcher(str);
while(m.find()){
int start = m.start();
int end = m.end();
str1 = str1 + str.substring(start,end) + " ";
}
String[] str2 = str1.split(" ");
for (int i = str2.length-1; i >=0; i--){
Pattern p1 = Pattern.compile("[^\\w+]");
Matcher m1 = p1.matcher(str2[i]);
if(m1.find()){
System.out.print(str2[i]);
}
else{
System.out.print(str2[i] + " ");
}
}
}
}
这个应该对任何字符串都适用的。
(1)正则
(2)split
(3)判断输出
[其他解释] 引用: 。。。晕,你不是说思路么。Java code?12345678910111213141516171819202122232425262728293031import java.util.regex.Matcher;import java.util.regex.Pattern; public class StringReverse { public static …… 好吧 你的太深奥了
[其他解释] 引用: 引用:引用: 改进了一下,这个应该没问题了。当然也存在一些地方需要改善,比如存在两个!什么的,这里懒一下,就不做了 Objective C code?1234567891011121314151617181920212223package com.briup.test08;public clas…… 可能是我的错了
[其他解释] 你只要看下正则表达式就会了,而且是对一般正规字符串都适用的。有运行过么?
[其他解释] 那是智商问题
[其他解释] 引用: 你只要看下正则表达式就会了,而且是对一般正规字符串都适用的。有运行过么? 行的 但是怎么让!号前面有个空格
[其他解释] 什么意思?怎么个形式
[其他解释] package com.tur.demo; public class Hello { public static void main(String[] args) { System.out.println(reverseWords("How are you!")); System.out.println(reverseWords("do justice to a dinner")); System.out.println(reverseWords("")); System.out.println(reverseWords("A")); System.out.println(reverseWords("A Biao")); System.out.println(reverseWords("!A Biao C.")); } public static String reverseWords(String str) { int start = 1; int end = 0; char[] chs = str.toCharArray(); reverseCharactersInRange(chs, 0, chs.length - 1); for (int i = 0; i < chs.length; ++i) { if (Character.isLetter(chs[i])) { if (start > end) { start = end = i; } else { ++end; } } else { if (start < end) { reverseCharactersInRange(chs, start, end); } start = chs.length; } } if (start < end) { reverseCharactersInRange(chs, start, end); } return new String(chs); } public static void reverseCharactersInRange(char[] chs, int start, int end) { int times = (end - start + 1) / 2; for (int i = 0; i < times; ++i) { char temp = chs[start + i]; chs[start + i] = chs[end - i]; chs[end - i] = temp; } } }
!you are How dinner a to justice do A Biao A .C Biao A! [其他解释] 存储字符串时最后多存一位,倒着输出就会有空格了..
[其他解释] 该回复于2012-11-23 08:42:47被管理员删除
[其他解释] (1)正则
(2)split
(3)判断输出
------其他解决方案--------------------
import java.util.regex.Matcher; import java.util.regex.Pattern; public class StringReverse { public static void main(String[] args) { System.out.print(" !you are How"); } }前面有空格,有木有!有木有!
[其他解释] 引用: 什么意思?怎么个形式 就是输出时!号 前面还有个空格
[其他解释] 引用: Java code?123456789import java.util.regex.Matcher;import java.util.regex.Pattern; public class StringReverse { public static void main(String[] args) { System.out.print(" !you…… 好牛啊
[其他解释] 引用: 。。。晕,你不是说思路么。 Java code? 12345678910111213141516171819202122232425262728293031 import java.util.regex.Matcher; import java.util.regex.Pattern; public class StringReverse { pub…… 正则表达式才强大,不然写起来很麻烦,有时间多去学学
[其他解释] 引用: Java code?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950package com.tur.demo; public class Hello { public static void main(String[] args) {…… 好厉害啊 我先研究研究
[其他解释] 用空格分隔写入数组,然后将数组从大到小输出,输出时带上空格。
[其他解释] 引用: 我就不说StringBuffer有个reverse方法 [其他解释] 虽然不懂,但觉得厉害
[其他解释] (1)! you are How
(2) !you are How
哪一种?
[其他解释] 引用: 那是智商问题 有本事你给写个 虽然我才学
但是还是感觉到你学的不怎么样
[其他解释] String str="how are you!"; StringBuffer buffer=new StringBuffer(str); System.out.println(buffer.reverse()); [其他解释] 引用: (1)! you are How (2) !you are How 哪一种? 第二种
[其他解释] 写个正则提取单词与标点,每个单词或者标点作为一个String,之后添加到Collections(比如List)调用Collections的reverse翻转就行里
[其他解释] package com.zr.csdn; import java.util.regex.Matcher; import java.util.regex.Pattern; public class StringReverse { public static void main(String[] args) { String str ="How are you!"; String str1 = ""; Pattern p = Pattern.compile("([a-zA-Z]+)[其他解释]
引用: Java code?12345678910111213public static void main(String[] args) { StringBuffer ss =new StringBuffer(); String s= "How are you! "; String[] a = s.split(" "); …… 谢谢 简单易懂
[其他解释] public static void main(String[] args) { StringBuffer ss =new StringBuffer(); String s= "How are you! "; String[] a = s.split(" "); for (int i = a.length-1; i >= 0; i--) { if(a[i].contains("!")){ a[i] = a[i].replace("!", ""); a[i]="!"+a[i]; } ss.append(" ").append(a[i]); } System.out.println(ss); } [其他解释] [\\w]+)").matcher(input);
List<String> l=new ArrayList<String>();
while(m.find()){
l.add(m.group(1));
}
return l.toArray(new String[0]);
}
/**
* 进行翻转的方法,每个单词或者标点为一个字符串
* @param input 想要翻转的字符数组
* @return
*/
public String[] reverse(String... input){
List<String> s=Arrays.asList(input);
Collections.reverse(s);
for(int i=0;i<s.size();i++){
if(s.get(i).matches("\\p{Punct}"))
s.set(i+1,s.get(i)+s.get(i+1));
}
return s.toArray(new String[0]);
}
}
不好意思,刚刚没有注意到你的特殊要求,输出结果是要这种“ !you are how”。不过觉得你这个需求略微有点怪,这是什么编程题目吗
[其他解释] [\\w]+)").matcher(input);
List<String> l=new ArrayList<String>();
while(m.find()){
l.add(m.group(1));
}
return l.toArray(new String[0]);
}
/**
* 进行翻转的方法,每个单词或者标点为一个字符串
* @param input 想要翻转的字符数组
* @return
*/
public String[] reverse(String... input){
List<String> s=Arrays.asList(input);
Collections.reverse(s);
return s.toArray(new String[0]);
}
}
[其他解释] import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ReverseWords { public static void main(String[] args){ ReverseWords test=new ReverseWords(); String[] result=test.reverse(test.spilt("how are you!")); for(int i=0;i<result.length;i++) System.out.print(result[i]+" "); } /** * 切分各个单词与标点 * @param input 待切分的句子 * @return */ public String[] spilt(String input){ Matcher m=Pattern.compile("([\\p{Punct}]+[其他解释] 创建一个String数组,用“ ”空格来隔开你的String。然后for循环,倒着输出就OK了[其他解释] public class Test { public static void main(String[] args) { String str = "How are you !"; String[] s = str.split(" "); for(int i = s.length-1;i>=0;i--){ System.out.print(s[i]+" "); } } }[其他解释]
引用: Java code?12345678910111213public static void main(String[] args) { StringBuffer ss =new StringBuffer(); String s= "How are you! "; String[] a = s.split(" "); …… 这种方法在这里完全可以用,但是觉得不是很有普遍性,如果把“!”换成其他标点的话,就要写很多判断了
[其他解释] 引用: 引用:Java code?12345678910111213public static void main(String[] args) { StringBuffer ss =new StringBuffer(); String s= "How are you! "; String[] a =…… 是啊针对性强
[其他解释] 引用: 代码写少了,方法见得少了,肯定没有思路了! 是啊 现在只能努力了
[其他解释] 不用自带库的话23楼正解
[其他解释] StringBuffer result = new StringBuffer();
List<String> list = new ArrayList<String>();
String str = "How are you! ";
Pattern pattern = Pattern.compile("\\w+
[其他解释] 引用: 引用:引用:Java code?12345678910111213public static void main(String[] args) { StringBuffer ss =new StringBuffer(); String s= "How are you! ";…… 噢噢,那要笔试的那就另当别论,不过,代码健壮性跟适应性也是考官所关注的,可以看出你的编程思维方法