英文:
Dose the if statement recognize the answer Scanner(System.in).nextLine() received?
问题
以下是翻译好的内容:
如果在输入 Y 作为语句时不起作用。
+如何读取和更改 config.cfg 文件?
我的代码如下。
```java
package myfirstpgram;
import java.io.*;
import java.util.*;
public class MidiBot {
public static void main(String args[]) throws InterruptedException, SecurityException, IOException {
File FolderDD = new File("./ProgramMF_Data"); // 将程序数据设置为变量(./ProgramMF_Data)
try {
FolderDD.mkdir(); // 创建文件夹 ProgramMF_Data
System.out.println("成功创建文件夹。"); // 打印成功创建文件夹
}
catch(Exception e) { // 捕捉错误
e.getStackTrace(); // ?
e.printStackTrace(); // 打印错误信息 1
System.out.println("错误1 - 无法创建目录。"); // 打印错误信息 2
System.exit(1);
}
System.out.println("您第一次运行程序吗? [Y/n]");
String FirstEM;
FirstEM = sc.nextLine();
if ("Y".equals(FirstEM)) {
System.out.println("请创建 ./ProgramMF_Data/config.cfg 文件");
System.out.println("并设置内容如下");
System.out.println("\n[Config]"); //
System.out.println("FirstTime=1"); //
Thread.sleep(60000); // 休眠 60 秒
System.out.println("\n程序将在 4 秒钟后关闭!"); // 信息
Thread.sleep(4000); // 休眠 4 秒
System.exit(0); // 关闭程序
}
System.out.println("再次欢迎"); // 打印 "再次欢迎"
}
}
<details>
<summary>英文:</summary>
If does not work when Y is entered as the statement.
+And how do I read and change config.cfg file?
My code is as below.
package myfirstpgram;
import java.io.;
import java.util.;
public class MidiBot {
public static void main(String args[]) throws InterruptedException, SecurityException, IOException {
File FolderDD = new File("./ProgramMF_Data"); // Set Program data to var(./ProgramMF_Data)
try {
FolderDD.mkdir(); // create Folder ProgramMF_Data
System.out.println("successfully created folder."); // print success to create folder
}
catch(Exception e) { //Catch error
e.getStackTrace(); // ?
e.printStackTrace(); // print error info 1
System.out.println("ERROR1 - Can't create Directory."); // print error info 2
System.exit(1);
}
System.out.println("Did you run the program for the first time? [Y/n]");
String FirstEM;
FirstEM = sc.nextLine();
if ("Y".equals(FirstEM)) {
System.out.println("Please Create ./ProgramMF_Data/config.cfg");
System.out.println("and set content like below");
System.out.println("\n[Config]"); //
System.out.println("FirstTime=1"); //
Thread.sleep(60000); // Sleep 60 seconds
System.out.println("\nProgram closes in 4 seconds!"); // info
Thread.sleep(4000); // sleep 4 seconds
System.exit(0); // Close program
}
System.out.println("Welcome again"); // print "Welcome again"
}
}
It looks like your post is mostly code; please add some more details.
</details>
# 答案1
**得分**: 1
你需要在使用之前声明和初始化一个扫描器。
在使用之前请添加 `Scanner sc = new Scanner(System.in);`。
我还建议您休息一下,查阅一些[命名约定][1]。
[1]: https://www.geeksforgeeks.org/java-naming-conventions/
<details>
<summary>英文:</summary>
You need to declare and initiaize a scanner before being able to use it
Please add `Scanner sc = new Scanner(System.in);` before using it.
I would also recommend you to take a small break and look up some [naming conventions][1].
[1]: https://www.geeksforgeeks.org/java-naming-conventions/
</details>
# 答案2
**得分**: 1
你的代码似乎是正确的,不过在输入小写字母 "y" 时可能会出现问题。
一个更好的方法是使用
"y".equalsIgnoreCase(FirstEM);
另外,你需要初始化 Scanner。
Scanner sc = new Scanner(System.in);
至于配置文件,如果是属性文件的话,可以参考
[这个链接][1]。
[1]: https://www.tutorialspoint.com/how-to-read-the-data-from-a-properties-file-in-java
<details>
<summary>英文:</summary>
your code seems correct, though an issue could arise when you enter a lower case "y".
a better approach would be
"y".equalsIgnoreCase(FirstEM);
And also you need to initialize scanner.
Scanner sc = new Scanner(System.in);
as for the config file, if it is a properties file then
[check this link][1]
[1]: https://www.tutorialspoint.com/how-to-read-the-data-from-a-properties-file-in-java
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论