如何使用 Java 方法返回的布尔值?
我有一个方法将一堆字符发送到另一个方法,如果存在某些字符,该方法将返回 true 或 false。一旦此方法评估所有字符并为每个字符返回 true 或 false,我如何在另一个方法中使用这些 true 或 false 值? 我想我有一次发送一个字符的方法。 Boolean 方法不是布尔数组。
public void Parse(String phrase)
{
// First find out how many words and store in numWords
// Use "isDelim()" to determine delimiters versus words
//
// Create wordArr big enough to hold numWords Strings
// Fill up wordArr with words found in "phrase" parameter
int len = phrase.length();
char[] SeperateString = new char[len];
for (int i = 0; i < len; i++) {
SeperateString[i] = phrase.charAt(i);
isDelim(SeperateString[i]);
System.out.println(SeperateString[i]);
boolean isDelim(char c)
{
{
if (c == delims[0])
return true;
else if (c == delims[1])
return true;
else if (c == delims[2])
return true;
else
return false;
}
//Return true if c is one of the characters in the delims array, otherwise return false
}
I have a method that sends a bunch of characters to another method that will return true or false if certain character are present. Once this method evaluates all the character and returns true or false for each of them how do I use those true or false vales in another method?
I think I have the method sending one character at a time. The Boolean method is not a boolean array.
public void Parse(String phrase)
{
// First find out how many words and store in numWords
// Use "isDelim()" to determine delimiters versus words
//
// Create wordArr big enough to hold numWords Strings
// Fill up wordArr with words found in "phrase" parameter
int len = phrase.length();
char[] SeperateString = new char[len];
for (int i = 0; i < len; i++) {
SeperateString[i] = phrase.charAt(i);
isDelim(SeperateString[i]);
System.out.println(SeperateString[i]);
boolean isDelim(char c)
{
{
if (c == delims[0])
return true;
else if (c == delims[1])
return true;
else if (c == delims[2])
return true;
else
return false;
}
//Return true if c is one of the characters in the delims array, otherwise return false
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以你的方法是这样的,
你可以稍后再这样做
,甚至
现在,如果你的方法返回很多布尔值,比如每个字符一个,它看起来更像这样
你必须以其他方式访问它们,也许像这样:
有关这方面的更多信息,您必须发布您的代码。
为了响应发布的代码,这就是我要做的
so your method is like this
you can do this later
or even
Now if your method is returning lots of booleans, say one for each character, it would look more like this
You'd have to access them some other manner, perhaps like so:
For more information on this you'd have to post your code.
In response to the posted code, here's what I would do
据我所知,您可以使用 String.split() 代替。
您也可以使用正则表达式,这样您就可以包含 3 个不同的分隔符。
如果您还需要作品数量,您可以获得结果的长度。
http:// /download.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29
假设 delims 是一个字符数组,那么使用它是安全的生成正则表达式:
From what I see, you could be using String.split() instead.
You can use regex with it as well, so you can include your 3 different delimiters.
If you need the number of works still you can get the result's length.
http://download.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29
Assuming delims is a character array, it would be safe to use this to generate the regex: