-4

我需要有关在 SAME PROGRAM 中使用文件(文件名是)输入inp2.dat和使用文件(文件名是)输出的帮助。out2.dat我使用FileInputStreamFileOutputStream在同一个班级吗?请帮忙。它的输出文件未找到。

import java.io.*;
class MBF
{
static String fileinp="inp2.dat";
public void main() 
{
    boolean EOF=false;
    try
    {
        FileInputStream fr=new FileInputStream(fileinp);
        DataInputStream dr=new DataInputStream(fr);
        while(!EOF)
        {
            try
            {
    System.out.println("Enter no. of inputs:");
    int n=dr.readInt();
    int max=0;
    for(int a=1;a<=n;a++)
    {
        System.out.println("Enter:");
        int p=dr.readInt();
        String str=dr.readLine();
        max=max+1;
        if(str.charAt(1)==str.charAt(0))
        max=max+1;
        else
        max=max+2;
        for(int i=0;i<p-2;i++)
        {
            char f=str.charAt(i);
            char s=str.charAt(i+1);
            char t=str.charAt(i+2);
            if((f==s)&&(f==t))
            max=max+1;
            else
            if(((f==s)&&(f!=t))||((s==t)&&(f!=t))||((f==t)&&(t!=s)))
            max=max+2;
            else
            max=max+3;
        }
        max=0;
      }
    }
    catch(EOFException el)
    {
        System.out.println("end of file");
        EOF=true;
    }
    catch(IOException e)
    {
        System.err.println(e);
    }
    }
    }
    catch(FileNotFoundException e)
    {
        System.out.println("File not found");
    }
   }
}
4

1 回答 1

0

整个过程中存在一些问题。我对评论进行了一些更改:

class MBF
{    
    static String fileinp = "C:\\inp2.dat";

    public static void main(String[] args) // main method signature was wrong 
    {
        // this line doesn't need to be outside of main, and you aren't using your fileinp string
        Scanner in = new Scanner(new File(fileinp)); 

        // added your writer
        BufferedWriter out = new BufferedWriter(new FileWriter("C:\\out2.dat"));
        System.out.println("Enter no. of inputs:");
        int n = in.readInt(); 
        //in.close(); I don't think you meant to do this here.
        int max=0;

        for(int a = 1; a <= n; a++)
        {
            System.out.println("Enter:");

            // these were originally dr.read... dr is not a variable in scope here. I think you meant in
            int p = in.readInt();
            String str = in.readLine();

            max=max+1;
            if(str.charAt(1)==str.charAt(0))
                max=max+1;
            else
                max=max+2;

            for(int i = 0; i < p - 2; i++)
            {
                char f = str.charAt(i);
                char s = str.charAt(i + 1);
                char t = str.charAt(i + 2);

                if ((f == s) && (f == t))
                    max=max+1;
                else if (((f == s) && (f != t)) || ((s == t) && (f != t)) || ((f == t) && (t != s)))
                    max=max+2;
                else
                    max=max+3;
            }

            out.write(max + "\n");

            max=0;
        }

        in.close();
        out.close();
    }
}

应该做你想做的。

于 2013-07-27T19:06:30.343 回答