Java - "varible name"无法解析为变量 - 我找不到错误在哪里



我已经阅读了其他标题相同的问题,但没有任何帮助,但在线没有任何帮助。

我是Java的新手,正在尝试运行基本程序,但我一直在遇到上述错误。

代码下面。

package loopy;
import java.io.*;
public class loopy {
    public static void main (String[] args) {
        // TODO: Use a loop to print every upper case letter
        for (int i = 65; i < 91; i++) {
            System.out.println((char)i);
        }
        // TODO: Get input from user. Print the same input back but with cases swapped.
          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
          try {
            String input = in.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        toggleStringCase(input);

    }
    // TODO: Implement this function to return the opposite case of the letter given. DO NOT USE any built in functions.
    // How to handle the case where the char given is not a letter?
    private static char toggleCase(char c) {
        return c;
    }
    // TODO: Implement this function to toggle the case each char in a string. Use toggleCase() to help you.
    private static String toggleStringCase(String str) {
        return str;
    }
}

它说togglestringcase(输入);是我遇到错误试图将变量传递到函数的地方。

我没有读过的任何东西都建议我做错了什么。

我确定这一定是一个基本错误,但是有人可以指向我正确的方向。

我错过了某个地方的语法吗?

input仅在 try块中具有范围,在此处移动呼叫。另外,我更喜欢使用另一个尝试块明确关闭intry-with-resources,应该注意的是,关闭in还关闭System.in(这是一个全局变量),并且应该非常谨慎(因为从System.in中阅读的任何未来尝试都会失败)

try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) {
    String input = in.readLine();
    toggleStringCase(input);
} catch (IOException e) {
    e.printStackTrace();
}

您必须将变量[输入]移至范围。因为您在尝试块中声明它,但要在范围中使用。

public class loopy {
public static void main (String[] args) {
    // TODO: Use a loop to print every upper case letter
    for (int i = 65; i < 91; i++) {
        System.out.println((char)i);
    }
    String input=null;
    // TODO: Get input from user. Print the same input back but with cases swapped.
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    try {
        input = in.readLine();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    toggleStringCase(input);

}
// TODO: Implement this function to return the opposite case of the letter given. DO NOT USE any built in functions.
// How to handle the case where the char given is not a letter?
private static char toggleCase(char c) {
    return c;
}
// TODO: Implement this function to toggle the case each char in a string. Use toggleCase() to help you.
private static String toggleStringCase(String str) {
    return str;
}

}

变量input无法求解到变量,因为您在main方法的范围中没有input变量(您使用input变量作为参数的范围(范围)toggleStringCase方法)。您仅在try的范围中具有input变量,这意味着input变量仅在try中可以访问,并且由于您在try之外使用input变量,这就是为什么它会产生错误。

有两种可能的方法来解决此问题:

  • 要解决此问题,您应该在main方法的范围内移动input变量的声明。我在下面更新了您的代码:
package loopy;
import java.io.*;
public class loopy {
    public static void main (String[] args) {
        // TODO: Use a loop to print every upper case letter
        String input = ""; // DECLARE input HERE so that it can be used in the scope of your main method
        for (int i = 65; i < 91; i++) {
            System.out.println((char)i);
        }
        // TODO: Get input from user. Print the same input back but with cases swapped.
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        try {
            input = in.readLine(); // get the actual input
        // The try/catch below are commented out since you can combine it to the try/catch above
        // START
        //} catch (IOException e) {
        //    // TODO Auto-generated catch block
        //    e.printStackTrace();
        //}
        //try {
        // END
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        toggleStringCase(input);

    }
    // TODO: Implement this function to return the opposite case of the letter given. DO NOT USE any built in functions.
    // How to handle the case where the char given is not a letter?
    private static char toggleCase(char c) {
        return c;
    }
    // TODO: Implement this function to toggle the case each char in a string. Use toggleCase() to help you.
    private static String toggleStringCase(String str) {
        return str;
    }
}
  • ,您可以在try-catch中移动toggleStringCase的功能调用。请参阅下面的代码。
package loopy;
import java.io.*;
public class loopy {
    public static void main (String[] args) {
        // TODO: Use a loop to print every upper case letter
        for (int i = 65; i < 91; i++) {
            System.out.println((char)i);
        }
        // TODO: Get input from user. Print the same input back but with cases swapped.
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        try {
            String input = in.readLine(); // get the actual input
            toggleStringCase(input); // MOVE IT HERE
        // The try/catch below are commented out since you can combine it to the try/catch above
        // START
        //} catch (IOException e) {
        //    // TODO Auto-generated catch block
        //    e.printStackTrace();
        //}
        //try {
        // END
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // toggleStringCase(input); // was moved inside try-catch

    }
    // TODO: Implement this function to return the opposite case of the letter given. DO NOT USE any built in functions.
    // How to handle the case where the char given is not a letter?
    private static char toggleCase(char c) {
        return c;
    }
    // TODO: Implement this function to toggle the case each char in a string. Use toggleCase() to help you.
    private static String toggleStringCase(String str) {
        return str;
    }
}

经典范围问题。VAR输入仅在Try Block内部或Bracs {}下方的内容才能访问 将toggleStringCase(input);移动到输入本身的尝试块中

public class loopy {
    public static void main (String[] args) {
        // TODO: Use a loop to print every upper case letter
        for (int i = 65; i < 91; i++) {
            System.out.println((char)i);
        }
        // TODO: Get input from user. Print the same input back but with cases swapped.
          BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
          try {
            String input = in.readLine();
            toggleStringCase(input); // moved
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          try {
            in.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

,或者您可以将字符串输入在try block外声明,并具有一些默认/init值,例如

String input = "default value";// moved
try {
    input = in.readLine();
    toggleStringCase(input); 
}

最新更新