Проблема с моим кодом - PullRequest
0 голосов
/ 03 июля 2018

У меня проблема с моим кодом. Я знаю, что это связано с конструктором Count и его параметрами. Однако я не могу сосредоточиться на этом.

Какие параметры я должен добавить в конструктор Count, чтобы код работал?

import java.util.Scanner;
public class CH9Assignment
{
    public static void main (String [] args)
    {
        count();
        //Create Scanner object
        Scanner input = new Scanner (System.in) ;
        System.out.println ("Enter a sentence:");
        String s1 = input.nextLine();
        System.out.println ("Enter 1 to count characters, 2 to count words");
        int x = input.nextInt();
        if (x == 1)
            System.out.println ("There are " + s1.length() + "characters");
    }


    public static int count(String word) 
    {
        if (word == null || word.isEmpty()) 
        { 
            return 0; 
        } 
        int wordCount = 0; 
        boolean isWord = false; 
        int endOfLine = word.length() - 1; 
        char[] characters = word.toCharArray(); 
        for (int i = 0; i < characters.length; i++) 
        { 
            if (Character.isLetter(characters[i]) && i != endOfLine) 
            { 
                isWord = true; 
            } 
            else if (!Character.isLetter(characters[i]) && isWord) 
            { 
                wordCount++; isWord = false;
            } 
            else if (Character.isLetter(characters[i]) && i == endOfLine) 
            { 
                wordCount++; 
            }

            if (x == 2)
                System.out.println ("There are " + wordCount + "words"); 

            }

    }
}

Ответы [ 2 ]

0 голосов
/ 03 июля 2018

У вас есть только один метод с именем count, для него требуется параметр String, но вы пытаетесь вызвать его без параметров.

import java.util.Scanner;
public class CH9Assignment
{
    public static void main (String [] args)
    {
      //  count(); --> DELETE THIS CALL, you don't have such a method
    //Create Scanner object
    Scanner input = new Scanner (System.in) ;
    System.out.println ("Enter a sentence:");
    String s1 = input.nextLine();
    int result = count(s1); // ADD THIS LINE
    System.out.println ("Enter 1 to count characters, 2 to count words");
    int x = input.nextInt();
    if (x == 1)
        System.out.println ("There are " + s1.length() + "characters");
}

// Your count method

}

Это решит проблемы компиляции. Вы должны быть в состоянии решить логические проблемы.

0 голосов
/ 03 июля 2018

Вы должны вызвать метод count с чем-то в качестве параметра. Так что просто вызовите его с s1 в качестве параметра внутри блока if. Ваш count метод также кажется слишком большим и взорванным. Поэтому я изменил его, чтобы просто разделить строку простым регулярным выражением. Попробуйте следующий фрагмент:

public static void main (String [] args){
    //Create Scanner object
    Scanner input = new Scanner (System.in) ;
    System.out.println ("Enter a sentence:");
    String s1 = input.nextLine();
    System.out.println ("Enter 1 to count characters, 2 to count words");
    int x = input.nextInt();
    switch(x){
        case 1;
            System.out.println ("There are " + s1.length() + " characters");
            return;
        case 2:
            System.out.println ("There are " + count(s1) + " words");
            return;
    }
}

public static int count(String word){
    if(word == null || word.isEmpty()){
        return 0;
    }
    return word.split("(\\s|\\S)+").length;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...