![]() |
||
|
Master Member
![]() ![]() ![]() ![]() 加入日期: Apr 2001 您的住址: Coruscant
文章: 1,637
|
問個Java程式的問題
最近看不少人問程式問題,
如果你覺得很簡單,請不要罵我,幫幫忙吧。 這支程式,我已經想了一個禮拜了。 我想做反字串的功能。 例如: 輸入:Hi~ what's your name? 輸出:iH~ tahw's ruoy eman? 但是以下的程式只會 輸出:iH~ s'tahw ruoy eman? import javax.swing.*; import java.util.*; class ReverseString{ public static void main(String[] args){ String name = JOptionPane.showInputDialog(null,"Enter a string"); StringTokenizer tokener = new StringTokenizer ( name , " " ) ; StringBuffer inputStringBuffer = new StringBuffer(); StringBuffer inputStringBuffer2 = new StringBuffer(); while ( tokener.hasMoreTokens() ){ //用token拆單字,以空格為區隔 String s = tokener.nextToken(); for(int i = 0; i < s.length(); i++) { char letter = s.charAt(i); //依序讀入單字的字元 if (((int)letter>=65&&(int)letter<=90)||((int)letter>=97&&(int)letter<=122)) { //如果是英文字母就依序排好 inputStringBuffer2.append(letter); }else{ inputStringBuffer.append(letter); //如果不是英文字母也是依序排好 } } inputStringBuffer2 = inputStringBuffer2.reverse(); //將單字的英文字母整串相反 System.out.print(" "+inputStringBuffer2+inputStringBuffer); inputStringBuffer2.setLength(0); //清空暫存器的字元 inputStringBuffer.setLength(0); } } } 加上一點註解,應該會好讀一點 ![]()
__________________
Un Jedi ne doit connaitre ni la colere, ni la haine, ni l'amour. Someday I will be the most powerful Jedi ever~!! Jedi Code there is no emotion;there is the peace there is no ignorance;there is the knowledge there is no passion;there is the serenity there is no death;there is the Force 此文章於 2004-05-14 06:36 PM 被 Tirpitz 編輯. |
|||||||
|
|
|
Advance Member
![]() ![]() 加入日期: Mar 2002 您的住址: 台灣
文章: 471
|
小弟不是很會剛學沒多久我也沒寫過你這種,我看到題目我的想法是這樣!!給你參考
一下!!錯了請別見怪!! 我是想用依序讀入讀到空白或是「'」這個符時停止,然後翻倒過來後放到陣列當temp把空白或「'」加上再讀入再放到temp陣列最後印出temp陣列這樣應該 能解決吧?我沒寫過這種的我不是很清楚
__________________
人生中有太多的選擇題,你能做最好的選擇嗎? |
||
|
|
|
Master Member
![]() ![]() ![]() ![]() 加入日期: Apr 2001 您的住址: Coruscant
文章: 1,637
|
謝謝你。
我現在也是想說把個別單字放到陣列中處理應該會好一點。 不過具體一點的做法還在想。(思考緩慢... )好多迴圈唷∼看的頭都暈了。
__________________
Un Jedi ne doit connaitre ni la colere, ni la haine, ni l'amour. Someday I will be the most powerful Jedi ever~!! Jedi Code there is no emotion;there is the peace there is no ignorance;there is the knowledge there is no passion;there is the serenity there is no death;there is the Force |
|
|
|
Advance Member
![]() ![]() 加入日期: Mar 2002 您的住址: 台灣
文章: 471
|
迴圈多只要你頭腦清楚應該都ok!!寫程式有縮排會比較好!!
加油!!我也半年沒寫了 ![]()
__________________
人生中有太多的選擇題,你能做最好的選擇嗎? |
|
|
|
Basic Member
加入日期: Aug 2003 您的住址: 洛杉磯
文章: 19
|
遇到不是字母就要斷喔^^
加油 功課吧? |
|
|
|
Master Member
![]() ![]() ![]() ![]() 加入日期: Apr 2001 您的住址: Coruscant
文章: 1,637
|
對呀。如果不是功課或是靠這吃飯的,誰會浪費外面好幾個晴朗的天空...
不說說是功課的感覺很怪齁,好像想坐享其成,上來丟問題,等收網。 我可先說,老師一開始只教了這個: import javax.swing.*; public class reversewholestring{ public static void main(String args[]) { String input; StringBuffer output; char letter; input = JOptionPane.showInputDialog(null, "Enter a sentence:"); output = new StringBuffer(input); output = output.reverse(); System.out.println(output); System.exit(0); } } 這是反轉整個字串的。 例如:Hi! I am Tirpitz. ---> .ztipriT ma I !iH 然後就要求這個。 本來還寫了一個又臭又長的程式,大概快100行了。一大堆迴圈抓單字。 後來翻書翻翻∼發現有這個Token的工具可以用。 又爬文爬很久找到怎麼清StringBuffer。這些他都沒有教過...課本上也輕描淡寫的。 所以啊,我現在遇到瓶頸了。只是想說這裡臥虎藏龍的, 有沒有高手可以點破一下關鍵點,或是提示一下其他有用的工具。 指點迷津... ps.我有縮排啦∼只是貼上來就都不見了...沒辦法。
__________________
Un Jedi ne doit connaitre ni la colere, ni la haine, ni l'amour. Someday I will be the most powerful Jedi ever~!! Jedi Code there is no emotion;there is the peace there is no ignorance;there is the knowledge there is no passion;there is the serenity there is no death;there is the Force |
|
|
|
*停權中*
加入日期: Jun 2001
文章: 989
|
ur problem:
"what's"->reverse()->"s'tahw" u can try: "what"->reverse()->"tahw" "s"->reverse()->"s" "tahw"+ ' +"s"=>>"tahw's" 看得懂嗎?... 你翻轉單字的就這一行,前面那一大段對我來說太複雜了 從那邊改有點難inputStringBuffer2 = inputStringBuffer2.reverse(); 所以是不是只要再前面加一個判斷 inputStringBuffer2裡面是不是有 ' 有的話,把它拆成兩個字在分開丟給reverse()處理 個別處理完再用 ' 合併起來 你可以試試看,我隨便想的方法 |
|
|
|
Master Member
![]() ![]() ![]() ![]() 加入日期: Apr 2001 您的住址: Coruscant
文章: 1,637
|
嗯,感謝你的意見呀。
之前也有人跟我提到這個方法。 你隨便想想就想到了耶。超強的∼真是佩服。 不過,ㄟ....取substring,有點模糊。 因為不光是" ' "這符號,這是我想出來的範例。 所有的標點都不可以動。想用ascii code處理。 所以,現在先不管之前的程式了。 我先想一個可以處理文字和符號的就行了。 簡化一下問題。 我做好了會po出全部的code,以後有人需要可以參考啦。 (搞不好有高手在旁偷笑唷∼... )
__________________
Un Jedi ne doit connaitre ni la colere, ni la haine, ni l'amour. Someday I will be the most powerful Jedi ever~!! Jedi Code there is no emotion;there is the peace there is no ignorance;there is the knowledge there is no passion;there is the serenity there is no death;there is the Force |
|
|
|
Advance Member
![]() ![]() 加入日期: Jun 2003 您的住址: Taipei
文章: 442
|
先給你個腦力激盪的機會 - 如果你不急著交作業的話:
先想辦法分割一個字串,以"非 a-z 和 A-Z" 的字元為分界,再個別存起來,例如你舉的例子: Hi~ what's your name? 切割成: ["Hi", "~", " ", "what", "'", "s", "your", " ", "name", "?"] <-- 這裡我以陣列表示 再根據陣列每個元素去作 reverse - 可以判斷一下哪些要 reverse,哪些不用。 再把整個陣列印出來就成囉~ ![]()
__________________
現今世道,自爆文當故事書,站長的話做成語錄,幾百年前的文章嘛沒事就挖出來考古...
|
|
|
|
*停權中*
加入日期: Jun 2001
文章: 989
|
我不強啦
之前老師也丟個作業 是把輸入的字串抓出輸入幾個單字 要交兩份,一個不能用StringTokens,一個使用StringTokens 第一份就是一個字元一個字元去判斷啦,一個for就可以搞定了 所以你不要用StringTokens去做,你應該就不會遇到這些"想太多"的問題~ 就像上面的仁兄說的方法. 這種小作業用基本的方法想不會太難 |
|
|