在 if-else 分支中,我声明了两个具有相同名称的不同 PrintStream 对象。但是,当我稍后使用 PrintStream 对象时,编译器会说它“找不到符号”。为什么它看不到我创建的对象?该对象与声明它的 if-else 分支一起工作。代码如下:
System.out.println("Welcome. Would you like to continue a previous adventure, or begin anew?\n(type: new or old)");
status = scan.nextLine();
if(status.equals("new")) {
System.out.println("What do you call yourself?");
name = scan.nextLine();
System.out.println("And what shall be the title of your saga, after you are gone?");
game = scan.nextLine();
System.out.println("Very well. Prepare yourself, for this is the beginning of your end...\n...\n...\nAre you ready?");
status = scan.nextLine();
System.out.println("Not that it matters. Let us begin.");
status = scan.nextLine();
File file = new File(game + ".txt");
PrintStream print = new PrintStream(file);
print.println(name);
old = false;
} else {
System.out.println("So you're still alive? What was the title of your tale?");
game = scan.nextLine();
File file = new File(game + ".txt");
Scanner gscan = new Scanner(file);
String save = "";
while (gscan.hasNextLine()) {
save += gscan.nextLine();
save += "\n";
}
System.out.println(save);
PrintStream print = new PrintStream(file);
print.println(save);
old = true;
name = scan.nextLine();
}
if(old) {
}
print.println("beans");