0

我编写了这段代码来隐藏和取消隐藏任何文件或文件夹,但是当用户输入错误时如何显示错误,我尝试使用 else if 但逻辑代码错误,当用户在选择隐藏时输入错误时我想显示错误或取消隐藏,如果用户提供了错误的路径来隐藏或取消隐藏文件。

import java.io.*;
class k
{

    public static void main(String args[])
    {
        String a,h;
        boolean q=true;
        while(q==true){
            try{
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                System.out.print("\nunhide/hide/exit (u/h/e): ");
                h=br.readLine();
                if("hide".equalsIgnoreCase(h)){

                    System.out.print("\nwhat you want 2 hide: ");
                    a=br.readLine();


                    if(a.equals(a)){
                        Runtime r=Runtime.getRuntime();
                        Process p=r.exec("cmd.exe /c attrib +h "+a);
                        System.out.print("HIDDEN SUCCESSFULLY");
                    }

                        //else if(!a.equals(a)){System.out.print("error");}
                }else if("unhide".equalsIgnoreCase(h)){
                    System.out.print("what u want to unhide: ");
                    a=br.readLine();
                    if(a.equals(a)){
                        Runtime r=Runtime.getRuntime();
                        Process p=r.exec("cmd.exe /c attrib -h "+a);
                    }
                    System.out.print("UNHIDDEN SUCCESSFULLY");
                }

                else if("exit".equalsIgnoreCase(h)){
                    System.exit(1);
                }
            }catch(Exception e){
                System.out.println(e);
            }
        }
    }
}
4

2 回答 2

2

首先:您让用户从 3 个字母(h, u, e)中进行选择,因此您需要确保用户输入 "h" 而不是 "hide" ,因此您的检查需要是

   if ("h".equalsIgnoreCase(h)) //h not hide {
    .....
}

第二:很容易检查您输入的路径是否存在,所以如果它是错误的,您可以知道,像这样:

if (new File(a).exists()) {
      Process p = r.exec("cmd.exe /c attrib +h " + a);
      System.out.print("HIDDEN SUCCESSFULLY " + p);

} else {
      System.out.println("wrong input");
}
于 2013-09-17T15:04:29.840 回答
1

验证选项选择

您已经完成了第一条错误消息的 90%,请记住,如果您有一系列错误消息,if{} else if{} else if {}else{}那么任何不适合 ifs 的内容都会出现在 else 中。所以

if("hide".equalsIgnoreCase(h)){

    System.out.print("\nwhat you want 2 hide: ");
    a=br.readLine();


    if(a.equals(a)){ //<-----a.equals(a) is not valid validation!
        Runtime r=Runtime.getRuntime();
        Process p=r.exec("cmd.exe /c attrib +h "+a);
        System.out.print("HIDDEN SUCCESSFULLY");
    }else{
        System.out.println("Invalid Input");
    }

}

}else if("unhide".equalsIgnoreCase(h)){
    System.out.print("what u want to unhide: ");
    a=br.readLine();
    if(a.equals(a)){
        Runtime r=Runtime.getRuntime();
        Process p=r.exec("cmd.exe /c attrib -h "+a);
    }
    System.out.print("UNHIDDEN SUCCESSFULLY");
}else if("exit".equalsIgnoreCase(h)){
    System.exit(1);
}else{
    System.out.println("Invalid Input");
} 

这将在输入无效选项时给出错误消息,但您现有的文件名验证不正确,因此如果没有“不正确”的定义,则无法生成错误消息。

路径验证

我猜任何触发 catch 块的东西都是无效的,所以在循环内移动 try catch 并使用它来验证输入;

if("hide".equalsIgnoreCase(h)){

    System.out.print("\nwhat you want 2 hide: ");
    a=br.readLine();


    try{
        Runtime r=Runtime.getRuntime();
        Process p=r.exec("cmd.exe /c attrib +h "+a);
        System.out.print("HIDDEN SUCCESSFULLY");
    }catch(Exception e){
        System.out.print("Invalid path");
    }

}

其他要点

正如评论中所讨论的那样,if(a.equals(a))永远都是错误的。不管a 什么,它本质上就是它自己

于 2013-09-17T15:20:46.360 回答