如何使用 Java 方法返回的布尔值?

发布于 2024-12-07 11:52:49 字数 1058 浏览 0 评论 0原文

我有一个方法将一堆字符发送到另一个方法,如果存在某些字符,该方法将返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖 2024-12-14 11:52:49

所以你的方法是这样的,

boolean myMethod(String s) {
    // returns true or false somehow... doesn't matter how
}

你可以稍后再这样做

boolean b = myMethod("some string");

someOtherMethod(b);

,甚至

someOtherMethod(myMethod("some string"));

现在,如果你的方法返回很多布尔值,比如每个字符一个,它看起来更像这样

boolean[] myMethod(String s) {
    // generates the booleans
}

你必须以其他方式访问它们,也许像这样:

String str = "some string";
boolean[] bools = myMethod(str);
for(int i = 0; i < str.length(); i++) {
    someOtherMethod(bools[i]);
}

有关这方面的更多信息,您必须发布您的代码。

为了响应发布的代码,这就是我要做的

/**
 * 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;
}

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;
}
七月上 2024-12-14 11:52:49

据我所知,您可以使用 String.split() 代替。
您也可以使用正则表达式,这样您就可以包含 3 个不同的分隔符。
如果您还需要作品数量,您可以获得结果的长度。

http:// /download.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29

假设 delims 是一个字符数组,那么使用它是安全的生成正则表达式:

String regex = "[" + new String(delims) + "]";
String result = params.split(regex);

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:

String regex = "[" + new String(delims) + "]";
String result = params.split(regex);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文