尝试通过用户输入获取文件名,如果说“不”则打印消息想要写入文件
因此,如果用户想写入文件,我会尝试写一个文件名,并在不想写入文件的控制台ID中写“感谢您的播放”。
但是,无论我是输入“是”或“否”,它都不会给出文件提示,或者根据我的何处,将其打印的名称和年龄打印成无限循环。
我将在下面发布有关代码。
String answer2;
String write;
String fileName;
ArrayList<String> names = new ArrayList<>();
ArrayList<String> ages = new ArrayList<>();
// If I put it here, it will do a infinite loop of the names and ages inputted.
// Prompt the user if they want to write to a file.
System.out.println("Do you want to write to a file? ");
answer2 = keyboard.nextLine();
do
{
// If I put it here, it will continue to ask me the question infinitely.
// Prompt the user if they want to write to a file.
// System.out.println("Do you want to write to a file? ");
//answer2 = keyboard.nextLine();
}
while (answer.equals("no"));
do
{
// Prompt for the filename
System.out.println("Enter your filename: ");
fileName = keyboard.nextLine();
//break;
//PrintWriter outputFile;
try
{
PrintWriter outputFile = new PrintWriter(fileName);
// Write the name(s) and age(s) to the file
outputFile.println(names);
outputFile.println(ages);
outputFile.close();
}
catch (FileNotFoundException e)
{
//
e.printStackTrace();
break;
}
}
while (answer2.equals("yes"));
do
{
System.out.println("Thanks for playing!");
break;
}
while (answer2.equals("no"));
keyboard.close();
So, I'm trying to have the user write a filename if they want to write to a file, and write "Thanks for playing" in the Console id they don't want to write to a file.
However, no matter if I type in "yes" or "no", it will either not give the file prompt, or goes into an infinite loop with the names and ages printed, depending on where I put it.
I'll post the code in question below.
String answer2;
String write;
String fileName;
ArrayList<String> names = new ArrayList<>();
ArrayList<String> ages = new ArrayList<>();
// If I put it here, it will do a infinite loop of the names and ages inputted.
// Prompt the user if they want to write to a file.
System.out.println("Do you want to write to a file? ");
answer2 = keyboard.nextLine();
do
{
// If I put it here, it will continue to ask me the question infinitely.
// Prompt the user if they want to write to a file.
// System.out.println("Do you want to write to a file? ");
//answer2 = keyboard.nextLine();
}
while (answer.equals("no"));
do
{
// Prompt for the filename
System.out.println("Enter your filename: ");
fileName = keyboard.nextLine();
//break;
//PrintWriter outputFile;
try
{
PrintWriter outputFile = new PrintWriter(fileName);
// Write the name(s) and age(s) to the file
outputFile.println(names);
outputFile.println(ages);
outputFile.close();
}
catch (FileNotFoundException e)
{
//
e.printStackTrace();
break;
}
}
while (answer2.equals("yes"));
do
{
System.out.println("Thanks for playing!");
break;
}
while (answer2.equals("no"));
keyboard.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
do
/while
循环做。好吧,如果需要,请使用字典。它显然循环。它一直在括号中进行操作,直到wher子句不再是真的。您的代码将不断询问。如果,则需要
,而不是
do/while
循环。A
do
/while
loop does.. well, use a dictionary if you have to. It obviously loops. It keeps doing the thing in the brackets until the while clause is no longer true. Your code will continually keep asking.You want an
if
, not ado/while
loop here.