Java 为什么语句没有被执行

Java 为什么语句没有被执行,java,Java,此程序要求用户输入文件名。如果 文件存在。如果不存在,他们可以选择创建文件 或者输入另一个文件名进行搜索。但似乎有一条语句没有被执行 import java.io.PrintStream; import java.util.Scanner; import java.io.File; import java.io.IOException; class usingexist { static Scanner in=new Scanner(System.in); static Str

此程序要求用户输入文件名。如果 文件存在。如果不存在,他们可以选择创建文件 或者输入另一个文件名进行搜索。但似乎有一条语句没有被执行

import java.io.PrintStream;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;

class usingexist
{
    static Scanner in=new Scanner(System.in);
    static String select="";
    static String search="";
    static int x;

    public static void main(String[] args)throws IOException
    {
        x=1;

        while(x==1)
       {
            System.out.println("Please the file name you want to search");
            search=in.nextLine();

            File f=new File(search);


            if(f.exists()) // check if file exists
            {
                System.out.println("File Found.");
                x=2;
            }
            else if(!f.exists())  // creates file if dont exsist
            {
                System.out.println("File Not Found.");
                System.out.println("Do you want to create the File ? (Y/N)");
                select=in.nextLine();

                if(select.equals('Y'))
                {
                 f.createNewFile();            // this statement is not beig executed
                 System.out.println("created succesfully");
                 x=2;
                }

            }
            else if (select.equals('N')) // prompts the user to enter another file name
            {
                x=1;
            }
      }

    }
}

在Y的周围加上双引号。这里比较的是两个字符串,而不是字符。

而且由于if语句嵌套不正确,您应该更改一些代码逻辑。你也应该看看break关键字

if(f.exists()) // check if file exists
{
    System.out.println("File Found.");
    x=2;
}
else // creates file if dont exsist
{
    System.out.println("File Not Found.");
    System.out.println("Do you want to create the File ? (Y/N)");
    select=in.nextLine();

    if(select.equals("Y"))
    {
        f.createNewFile();            // this statement is not beig executed
        System.out.println("created succesfully");
        x=2;
    }
    else if (select.equals("N")) // prompts the user to enter another file name
    {
        x=1;
    }
}

f.createNewFile未被执行。任何人都可以帮助。tnk学习如何使用调试器在将来将非常有用。此外,if select.equalsN语句应该位于if select.equalsY的else分支上,而不是if!f、 出口所有这些选票是从哪里来的O
if(f.exists()) // check if file exists
{
    System.out.println("File Found.");
    x=2;
}
else // creates file if dont exsist
{
    System.out.println("File Not Found.");
    System.out.println("Do you want to create the File ? (Y/N)");
    select=in.nextLine();

    if(select.equals("Y"))
    {
        f.createNewFile();            // this statement is not beig executed
        System.out.println("created succesfully");
        x=2;
    }
    else if (select.equals("N")) // prompts the user to enter another file name
    {
        x=1;
    }
}