有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

无法解析java“”如何解决此错误?

我是一个初学者,对Java的理解很差。我使用Dr.Java,我得到以下错误:

     "S cannot be resolved"

使用此Java程序:

import java.util.Scanner;

public class HDtest5

{
    public static int countWords(String str) {
        String words[] = str.split(" ");
        int count = words.length;
        return count;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String sentence = in.nextLine();
        System.out.print("Your sentence has " + countWords(sentence) + " words.");
    }

    {
        while (true) // have created infinite loop
        {

            if (s.equals("quit"))
            // if enterd value is "quit" than it comes out of loop

            {

                String[] words = s.split(" "); // get the individual words
                System.out.println("#words = " + words.length);
                for (int i = 0; i < words.length; i++)
                    System.out.println(s + "word[" + i + words[i] + " nchars = " + words[i].length());

            }
            System.out.println(s); // to Print string
            System.out.println(s.length()); // to print Entered string's length

        }
    }
}

是什么原因导致此错误消息? 我认为所有名为“s”的字符串应该一起工作,没有任何问题。你能帮我找出它的毛病吗?非常感谢您的帮助,非常感谢

另外,更清楚的是,我的程序的目的是输出字符总数、每个单词的字符数以及在文本窗口(扫描仪)中输入的文本的单词数。代码应该可以很好地工作,除了这个“s”字符串问题,它使得代码块不能一起工作


共 (5) 个答案

  1. # 1 楼答案

    您之所以会出现此错误,是因为您尚未声明任何变量,但您正在if条件中使用此变量。此外,您还错误地放置了{}大括号。 将主要方法更改为以下内容:

    注意:您的程序正在infinte循环中运行

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String s = in.nextLine();
        System.out.print("Your sentence has " + countWords(s) + " words.");
    
        while (true) // have created infinite loop
        {
    
            if (s.equals("quit"))
            // if enterd value is "quit" than it comes out of loop
    
            {
    
                String[] words = s.split(" "); // get the individual words
                System.out.println("#words = " + words.length);
                for (int i = 0; i < words.length; i++)
                    System.out.println(s + "word[" + i + words[i]
                            + " nchars = " + words[i].length());
    
            }
            System.out.println(s); // to Print string
            System.out.println(s.length()); // to print Entered string's length
    
        }
    }
    
  2. # 2 楼答案

    我想我已经找到了你想要实现的目标并修复了你的代码:

    import java.util.Scanner;
    
    public class HDtest5
    
    {
        public static int countWords(String str) {
            String words[] = str.split(" ");
            int count = words.length;
            return count;
        }
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
    
            while (true) // have created infinite loop
            {
                System.out.print("Enter a sentence: ");
                String sentence = in.nextLine();
                if (sentence.equals("quit")) {
                    // if enterd value is "quit" than it comes out of loop
                    break;
                }
                System.out.print("Your sentence has " + countWords(sentence) + " words.");
    
                String[] words = sentence.split(" "); // get the individual words
                System.out.println("#words = " + words.length);
                for (int i = 0; i < words.length; i++)
                    System.out.println("word[" + i + "] = " + words[i] + " nchars = " + words[i].length());
                }
                System.out.println(sentence); // to Print string
                System.out.println(sentence.length()); // to print Entered string's length
            }
            in.close();
        }
    }
    
  3. # 3 楼答案

    您尚未在代码中声明s。在main()方法的开头写一些类似的东西,String s=null;,然后在以后的代码中给它一些值

  4. # 4 楼答案

    在Java中,必须先声明一个变量,然后才能使用它

    缺少如下代码行:

    String s;
    

    顺便说一句,正如其他人所注意到的,您可能希望使用sentence而不是s。可能的值"quit"将在sentence变量中


    通过声明变量

    String personName;
    

    你说编译器:

    From now on, understand personName as a piece of memory capable of storing a String.

    编译器将为您完成一项伟大的工作-它不会让您为personName分配任何其他内容(如int),如果您键入了一个错误并在某处写入personMame,您将立即发现它,并且不会很晚在运行时发现它,因为您不知道代码为什么不能工作,比如在JavaScript中

  5. # 5 楼答案

    我想您应该使用sentence变量。 而且,对于while循环的每次迭代,您都不会读取变量

    也许你想要更多这样的东西

    public class HDtest5 {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while (true) { // have created infinite loop
                System.out.print("Enter a sentence: ");
                String sentence = in.nextLine();
                System.out.print("Your sentence has " + countWords(sentence) + " words.");
    
                if (sentence.equals("quit")) { // if enterd value is "quit" than it comes out of loop
                    break;
                } else {
                    String[] words = sentence.split(" "); // get the individual words
                    System.out.println("#words = " + words.length);
                    for (int i = 0; i < words.length; i++)
                        System.out.println(sentence + "word[" + i + words[i] + " nchars = " + words[i].length());
    
                }
                System.out.println(sentence); // to Print string
                System.out.println(sentence.length()); // to print Entered string's length
    
            }
            in.close();
        }
    
        private static int countWords(String str) {
            String words[] = str.split(" ");
            int count = words.length;
            return count;
        }
    }