簡體   English   中英

Java布爾值未設置為true

[英]Java boolean not being set to true

我的代碼接受用戶輸入的字符串,並返回單詞數和第一個單詞。 當用戶輸入一個空字符串時,我不想顯示“您的字符串中包含x個單詞”或“第一個單詞是x”,因此我創建了一個boolean但是未在我嘗試設置的方法中設置boolean它可以true 。我能獲得有關為什么或如何修復它的任何幫助都將非常有用。 謝謝!

public static void main(String[] args){
    boolean empty = false;
    Scanner in = new Scanner(System.in);
    System.out.print("Enter a string: ");
    String str = in.nextLine();

    if (empty == false) {
        System.out.println("Your string has " + getWordCount(str)+" words in it.");
        System.out.println("The first word is: " + firstWord(str));
    }
}

public static String firstWord(String input) {

    for(int i = 0; i < input.length(); i++)
    {
        if(input.charAt(i) == ' ')
        {
            return input.substring(0, i);
        }
    }

    return input; 
}    

 public static int getWordCount(String str){
       int count = 0;

       if(str != null && str.length() == 0){ 
           System.out.println("ERROR - string must not be empty.");
           empty = true;
       }

       else if (!(" ".equals(str.substring(0, 1))) || !(" ".equals(str.substring(str.length() - 1)))){

            for (int i = 0; i < str.length(); i++){

                if (str.charAt(i) == ' '){
                    count++;
                }
            }
            count = count + 1; 
        }
    return count;
    }
 }

您必須在這里重新考慮自己的邏輯(請參見以下代碼段):

  • 首先,您不需要empty變量
  • 您可以通過調用getWordCount方法來知道“ word”是否為空並將結果存儲在變量中( wordCount ?)。 然后,您可以通過執行wordCount > 0來檢查是否至少有一個單詞。

片段:

    public static void main(String[] args){
        // boolean empty = false;           // --> not needed
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String str = in.nextLine();
        final int wordCount = getWordCount(str);

        if (wordCount > 0) { // show message only if there is at least one word
            System.out.println("Your string has " + wordCount +" words in it.");
            System.out.println("The first word is: " + firstWord(str));
        }
    }

    public static String firstWord(String input) {
        // .. code omitted for brevity
    }

    public static int getWordCount(String str){
        int count = 0;

        if(str != null && str.length() == 0){
            System.out.println("ERROR - string must not be empty.");
            // empty = true; -->  not needed
        }

        // ... code omitted for brevity
        return count;
    }

您只需更改位置即可,以實現可變的可見性。

public static void main(String[] args) {
        boolean empty = false;
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String str = in.nextLine();

        if (str != null && str.length() == 0) {
            System.out.println("ERROR - string must not be empty.");
            empty = true;
        }

        if (empty == false) {
            System.out.println("Your string has " + getWordCount(str) + " words in it.");
            System.out.println("The first word is: " + firstWord(str));
        }
    }

public static String firstWord(String input) {

        for (int i = 0; i < input.length(); i++) {
            if (input.charAt(i) == ' ') {
                return input.substring(0, i);
            }
        }

        return input;
    }

public static int getWordCount(String str) {
        int count = 0;

        if (!(" ".equals(str.substring(0, 1))) || !(" ".equals(str.substring(str.length() - 1)))) {

            for (int i = 0; i < str.length(); i++) {

                if (str.charAt(i) == ' ') {
                    count++;
                }
            }
            count = count + 1;
        }
        return count;
    }

如果您真的想使用布爾值,請嘗試使用字符串參數創建一個布爾方法,該方法僅在字符串不為空時才返回true,然后將布爾值返回類型分配給empty布爾值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM