判断字符串是否为十六进制数字的递归方法 - Java
这是一个家庭作业问题,我遇到了一些麻烦。
编写一个递归方法来确定字符串是否为十六进制数字。 为您的方法编写 javadoc。 字符串是一个十六进制数字,如果每个字符都是 0或1或2或3或4或5或6或7或8或9 或a或A或b或B或c或C或d或D或e或E或f或f。
目前我能看到的测试就是字符串 0 处的字符是否是他给我的这些值之一,那么这部分是十六进制。
有什么提示或建议可以帮助我吗?
这是我到目前为止所拥有的:`
public boolean isHex(String string){
if (string.charAt(0)==//what goes here?){
//call isHex again on s.substring(1)
}else return false;
}
`
This is a homework question that I am having a bit of trouble with.
Write a recursive method that determines if a String is a hex number.
Write javadocs for your method.
A String is a hex number if each and every character is either
0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9
or a or A or b or B or c or C or d or D or e or E or f or f.
At the moment all I can see to test this is if the character at 0 of the string is one of these values he gave me then that part of it is a hex.
Any hints or suggestions to help me out?
This is what I have so far: `
public boolean isHex(String string){
if (string.charAt(0)==//what goes here?){
//call isHex again on s.substring(1)
}else return false;
}
`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
听起来你应该递归地迭代字符串中的字符,并通过递归调用返回当前字符是否在
[0-9A-Fa-f]
中的布尔 AND...Sounds like you should recursively iterate the characters in string and return the boolean AND of whether or not the current character is in
[0-9A-Fa-f]
with the recursive call...您已经收到了很多有用的答案。如果您想进一步训练您的递归技能(以及一般的 Java 技能),我建议您访问 Coding Bat。您会发现很多练习以及自动化测试。
You have already received lots of useful answers. In case you want to train your recursive skills (and Java skills in general) a bit more I can recommend you to visit Coding Bat. You will find a lot of exercises together with automated tests.
如果您正在寻找一个好的十六进制数字方法:
根据要求提供提示或建议:
你的方法签名应该看起来像这样
另外,不要忘记十六进制字符串可以以“0x”开头。这可能(更)难以做到,所以我会首先让常规功能正常工作。如果您稍后解决这个问题,请记住 0xABCD0x123 不应该通过。 :-)
关于 substring: 直接来自 String 类源码:
offset 是
int
类型的成员变量value 是
char[]
类型的成员变量及其调用的构造函数显然
是一个 O(1) 的方法,调用了一个 O(1) 的构造函数。它可以做到这一点,因为 String 是不可变的。您无法更改字符串的值,只能创建一个新字符串。 (让我们忽略诸如反射和sun.misc.unsafe之类的东西,因为它们是无关的解决方案!)由于它无法更改,因此您也不必担心其他线程会弄乱它,所以像乡村自行车一样到处走走是完全可以的。
If you're looking for a good hex digit method:
Hints or suggestions, as requested:
Your method signature should look something like this
Also, don't forget that hex strings can start with "0x". This might be (more) difficult to do, so I would get the regular function working first. If you tackle it later, try to keep in mind that 0xABCD0x123 shouldn't pass. :-)
About substring: Straight from the String class source:
offset is a member variable of type
int
value is a member variable of type
char[]
and the constructor it calls is
It's clearly an O(1) method, calling an O(1) constructor. It can do this because String is immutable. You can't change the value of a String, only create a new one. (Let's leave out things like reflection and
sun.misc.unsafe
since they are extraneous solutions!) Since it can't be changed, you also don't have to worry about some other Thread messing with it, so it's perfectly fine to pass around like the village bicycle.由于这是家庭作业,我只给出一些提示而不是代码:
编写一个始终测试字符串的第一个字符是否满足要求的方法。如果不是,则返回 false,如果是,则使用相同的 String 再次调用该方法,但缺少第一个字符。如果只剩下 1 个字符并且也是一个十六进制字符,则返回 true。
伪代码:
Since this is homework, I only give some hints instead of code:
Write a method that always tests the first character of a String if it fulfills the requirements. If not, return false, if yes, call the method again with the same String, but the first character missing. If it is only 1 character left and it is also a hex character then return true.
Pseudocode:
当递归解决问题时,您通常想要解决一小部分(“基本情况”),然后递归解决其余部分。
您已经弄清楚了基本情况 - 检查单个字符是否是十六进制。
现在您需要“递归其余部分”。
这里有一些用于反转字符串的伪代码(Python-ish) - 希望您会看到如何将类似的方法应用于您的问题(实际上,所有递归问题)
When solving problems recursively, you generally want to solve a small part (the 'base case'), and then recurse on the rest.
You've figured out the base case - checking if a single character is hex or not.
Now you need to 'recurse on the rest'.
Here's some pseudocode (Python-ish) for reversing a string - hopefully you will see how similar methods can be applied to your problem (indeed, all recursive problems)