我已经阅读了其他标题相同的问题,但没有任何帮助,但在线没有任何帮助。
我是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
块中具有范围,在此处移动呼叫。另外,我更喜欢使用另一个尝试块明确关闭in
的try-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);
}