如何使用 Java 方法返回的布尔值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7616155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use the Boolean values returned by a Java method?
提问by user973858
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.
我有一个方法可以将一堆字符发送到另一个方法,如果存在某些字符,该方法将返回 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
}
回答by corsiKa
so your method is like this
所以你的方法是这样的
boolean myMethod(String s) {
// returns true or false somehow... doesn't matter how
}
you can do this later
你可以稍后再做
boolean b = myMethod("some string");
someOtherMethod(b);
or even
甚至
someOtherMethod(myMethod("some string"));
Now if your method is returning lots of booleans, say one for each character, it would look more like this
现在,如果您的方法返回大量布尔值,例如每个字符一个,它看起来更像这样
boolean[] myMethod(String s) {
// generates the booleans
}
You'd have to access them some other manner, perhaps like so:
您必须以其他方式访问它们,也许像这样:
String str = "some string";
boolean[] bools = myMethod(str);
for(int i = 0; i < str.length(); i++) {
someOtherMethod(bools[i]);
}
For more information on this you'd have to post your code.
有关这方面的更多信息,您必须发布您的代码。
In response to the posted code, here's what I would do
为了回应发布的代码,这就是我要做的
/**
* Parse a string to get how many words it has
* @param s the string to parse
* @return the number of words in the string
*/
public int parse(String s) {
int numWords = 1; // always at least one word, right?
for(int i = 0; i < s.length(); i++) {
if(isDelim(s.charAt(i)) numWords++;
}
return numWords;
}
private boolean isDelim(char c) {
for(char d : delims) if(c == d) return true;
return false;
}
回答by xthexder
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.
据我所知,您可以改用 String.split() 。您也可以使用正则表达式,因此您可以包含 3 个不同的分隔符。如果您仍然需要作品的数量,您可以获得结果的长度。
http://download.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29
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:
假设 delims 是一个字符数组,使用它来生成正则表达式是安全的:
String regex = "[" + new String(delims) + "]";
String result = params.split(regex);