作者BlackMatrix (BlackMatrix)
看板java
标题[问题] 如何把Strings里面分成很多不同的东西
时间Fri Feb 5 16:27:23 2010
问题是, 我想把一个一串Strings加数字让他变得不一样
有四种条件:
The return value will be the ROT13 encoding of the input file. For our
purposes the ROT13 algorithm is defined as follows:
A) If the input is a lower case letter, the output is the lower case
letter plus 13 (with wrap-around, so 'n' becomes 'a')
B) If the input is a upper case letter, the output is the upper case
letter plus 13 (with wrap-around, so 'n' becomes 'a')
C) If the input is a number the output is the number plus 5 (with
wrap-around so '6' becomes '1')
D) If the input is neither letter nor number, the output is the same
as the input ('!' becomes '!' and ' ' becomes ' ')
也就是说:
假如我的文件, test.txt里面的一串字是1234那就会变成5678, 然後
1a2a3a4a 就会变成 5n6n7n8n 因为a+13会变成n, 大写跟小写都会
我现在可以用Scanner跟Reader来读取我的strings, 可是重点是我不会改变他们的数字或
是字母
问题1:
a+13会变成a13, 那要怎麽才能让他跳13个字母
111我+5会变成545454...非常诡异
问题2:
因为我之前有学过C++, 我就想说把Strings当成Array of Chars可是似乎没办法
所以我就把他全部都写到 char[] charArray = new char [encodedFile.length()]
然後利用.charAt(i) <== i 是用for loop
我现在所有的字母跟数字都在Array of Char里面, 可是我要怎麽用if statement来表示
A. 如果碰到字母要跳13位
B. 如果碰到数字要+5
目前我A完全不会做, B我这样写, charArray[5] += 5是没办法这样做的.
因为我似乎把Char当成Int来用, 所以...到底该怎麽办
我在网上查了很多资料, 我有用Replace可是完全没办法用
我的Code在底下:
http://nopaste.csie.org/a4a9b
第一次写Java, 买了Just Java2 可是没有帮助我太多
因为实在写不出来, 请大家麻烦指点指点
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 173.56.121.140
※ 编辑: BlackMatrix 来自: 173.56.121.140 (02/05 16:28)
※ 编辑: BlackMatrix 来自: 173.56.121.140 (02/05 16:37)
1F:推 ogamenewbie:版规 4.1 表示: 02/05 16:29
2F:→ adrianshum:嘘作业文 02/05 16:29
3F:推 PsMonkey:你的「也就是说」後面咧? 不要富间 or 太监阿~ 02/05 16:30
4F:→ superlubu:也就是说: 要砍文罗? 02/05 16:31
5F:→ BlackMatrix:不好意思, 我想说跳出来paste一下code 02/05 16:38
喔喔, 似乎有进展了:
if (Character.isLetter(ch))
...
if (Character.isDigit(ch))
...
if (Character.isSpaceChar(ch))
※ 编辑: BlackMatrix 来自: 173.56.121.140 (02/05 16:46)
我现在是可以把数字分辨出来哩, 呼呼, 花了我好久的时间
※ 编辑: BlackMatrix 来自: 173.56.121.140 (02/05 16:47)
谢谢adrianshum大大的提示, 加快了我写Code的速度
目前已经可以把全部的东西都做好除了wrap-around
http://nopaste.csie.org/d0aec
不过应该自己做得出来(希望)
※ 编辑: BlackMatrix 来自: 173.56.121.140 (02/05 17:09)
经过大家的帮忙, 我已经顺利的把code做完哩
成品:
http://nopaste.csie.org/29a17
谢谢大家, 谢谢
※ 编辑: BlackMatrix 来自: 173.56.121.140 (02/05 17:41)