尝试通过用户输入获取文件名,如果说“不”则打印消息想要写入文件

发布于 2025-01-20 12:21:39 字数 1732 浏览 4 评论 0原文

因此,如果用户想写入文件,我会尝试写一个文件名,并在不想写入文件的控制台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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南风起 2025-01-27 12:21:39

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 a do/while loop here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文