【问题标题】:Creating folders from user input using while or do while loop使用 while 或 do while 循环从用户输入创建文件夹
【发布时间】:2018-04-26 08:44:13
【问题描述】:

我在使用 Java 中的 while、do-while 循环创建多个文件夹时遇到问题。我尝试创建文件,但文件夹没有问题,但故事不同。 我使用的是 Scanner 和 BufferedReader。问题是,它的行为真的很奇怪。 如果我输入至少 3 个文件夹名称,它通常只会创建第一个,或者跳过第二个并创建第一个和第三个。最后它不会接受我的“退出”作为我的终点,它只会创建名为“退出”的文件夹并等待更多输入。 我已经在 google 中搜索过,但没有找到任何对我有帮助的东西。这里是代码,(case AddShelf):`package library;

public class Administrator {

    public static String help_msg = "[ Main Menu ]\r\n\r\nAddBook\r\nRemoveBook\r\nBookInfo\r\nAddShelf\nRemoveShelf\r\nShelfInfo";
    private static final DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

    public static void main(String[] args)throws IOException {
                        Date date = new Date();
                        System.out.println(sdf.format(date));
                System.out.println("Welcome to my book library\r\n\r\n"+"ADMINISTRATOR\r\n\r\n"+help_msg+"\r\n");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String operate = br.readLine();

    do {
        switch(operate) {
        case"AddBook":
            //chose shelf,add new book  
        case"RemoveBook":
            //chose shelf,remove book
        case"BookInfo":
            //chose shelf,chose book,show book info
        case"AddShelf":
            try {
                System.out.println("Enter the name of the shelf:");
                Scanner sc = new Scanner(System.in);
                String files = sc.nextLine();
                do {
                    File dir = new File("D:\\admir\\MyBookLibrary\\"+files);
                    dir.mkdirs();
                }while(!"Exit".equals(sc.next()));
                System.out.println("Shelf added successfully!");
            }catch(Exception e) {
                System.err.println("Unable to create new Shelf!");
            }
        case"RemoveShelf":
            //remove shelf
        case"ShelfInfo":
            //shelf info
            default:
                //if incorrect operation is entered,print message,back to choosing operations
        }
    }while(!"Exit".equals(br.readLine()));

    }
}

【问题讨论】:

    标签: java do-while mkdir


    【解决方案1】:

    我检查了代码做while有点困难,所以我把它改成了while try this code。

      import java.io.File;
      import java.text.DateFormat;
      import java.text.SimpleDateFormat;
      import java.util.Date;
      import java.util.Scanner;
    
      public class Administrator {
    
        public static String help_msg = "[ Main Menu ]\r\n\r\nAddBook\r\nRemoveBook\r\nBookInfo\r\nAddShelf\nRemoveShelf\r\nShelfInfo";
        private static final DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    
        public static void main(String[] args){
          Date date = new Date();
          System.out.println(sdf.format(date));
          System.out.println("Welcome to my book library\r\n\r\n"+"ADMINISTRATOR\r\n\r\n"+help_msg+"\r\n");
    
          Scanner sc = new Scanner(System.in);
          String operate;
    
          while(!"Exit".equals(operate = sc.nextLine())){
            switch(operate) {
              case"AddBook":
                //chose shelf,add new book
              case"RemoveBook":
                //chose shelf,remove book
              case"BookInfo":
                //chose shelf,chose book,show book info
                break;
              case"AddShelf":
                try {
                  System.out.println("Enter the name of the shelf:");
                  String files;
                  while(!"Exit".equals(files = sc.nextLine())){
                    File dir = new File("D:\\admir\\MyBookLibrary\\"+files);
                    dir.mkdirs();
                    System.out.println(files+"Shelf added successfully!");
                    System.out.println("Enter another name of the shelf:");
                  }
                  return;
                }catch(Exception e) {
                  System.err.println("Unable to create new Shelf!");
                }
                break;
              case"RemoveShelf":
                //remove shelf
              case"ShelfInfo":
                //shelf info
              default:
                //if incorrect operation is entered,print message,back to choosing operations
            }
          }
    
        }
      }
    

    【讨论】:

    • 谢谢,这完全解决了我的问题。我从来不明白那里的返回角色是什么,我想我仍然不完全理解,但代码现在可以工作了。
    • return 表示退出 while 循环
    猜你喜欢
    • 1970-01-01
    • 2021-07-18
    • 2017-04-03
    • 2016-07-19
    • 2016-02-21
    • 1970-01-01
    • 2020-01-18
    • 2021-05-13
    • 2019-05-19
    相关资源
    最近更新 更多