找不到符号编译错误



我想我格式化错误或存在逻辑错误,老实说我不知道。 我在程序底部评论了错误。 非常感谢所有的帮助。

public static void main (String args[]) {
    String userInput;
    Scanner kb = new Scanner (System.in);
    System.out.print("Enter a string of characters: ");
    userInput = kb.nextLine();
    int length = userInput.length();
    for (count=0; length<count; count++) {
          char letter=userInput.charAt(count);
          System.out.print(letter + " ");
        }
  }

错误:

javac "StringDown.java" (in directory: /home/user/Downloads)
StringDown.java:16: cannot find symbol
symbol  : variable count
location: class StringDown
    for (count=0; length<count; count++)
         ^
StringDown.java:16: cannot find symbol
symbol  : variable count
location: class StringDown
    for (count=0; length<count; count++)
                         ^
StringDown.java:16: cannot find symbol
symbol  : variable count
location: class StringDown
    for (count=0; length<count; count++)
                                ^
StringDown.java:18: cannot find symbol
symbol  : variable count
location: class StringDown
         System.out.print(userInput.charAt(count));
                                           ^
4 errors
Compilation failed.
for (count=0; length<count; count++)

除了其他推荐int count=0;的答案,我想你可能有一个逻辑问题。 假设您修复了其他人指出的内容,并执行以下操作:

for(int count=0; length<count; count++)

您只是在修改 count 的大小。 您永远不会修改length的大小。 并且您的支票是length<count,并且您正在使用++修改count。 这意味着将发生以下两件事之一。

  1. length >= 0时,您将永远不会进入for循环的主体。
  2. length < 0 时,您将进入一个无限循环,其中length<count将始终返回true

(鉴于您通过检查String的长度来设置length,您将始终获得此特定示例中的第一个场景。

你的 for 循环

for (count = 0; length < count; count++) {

那应该是

for (int count = 0; length < count; count++) {

告诉计数是一种int类型。

作为旁注:尽快切换到IDE。这样你就可以更专注于你的逻辑,而不是最终出现这些类型的编译错误

堆栈跟踪告诉您错误存在的位置。变量需要先使用 type 关键字声明,然后才能使用。添加 int 关键字,以便可以在循环中使用变量count

for (int count = 0; length < count; count++) {
     ^
缺少

count as int 的声明,下面是正确的代码。

 for (int count=0; length<count; count++)

相关内容

最新更新