我有一个家庭作业,我需要根据用户输入的内容输出一个文本 ASCII 文件。我没有使用直接路径,因为当我把它交给教授时需要能够测试它,所以我在 src 文件夹中有文本文件,所以可以使用文件名。但是,当我尝试对其进行测试时,我收到了一个找不到文件的错误。我知道我正在正确输入文件名,但是我的代码中是否缺少导致此问题的内容?我正在尝试测试程序并确保它完成了它应该做的一切,但我有点卡住了,因为我无法让文件输出..这是我的代码:
import java.io.*;
import java.util.*;
class TileMap
{
/*
FUNCTION NAME: Main ;
INPUT: none.
OUTPUT: a message to the user of this program, all of the
prompts and a final display according to user specifications.
PRECONDITIONS: None.
POSTCONDITIONS: Variables and calls made according to users input
output set to start on a new line.
CALLERS: None.
CALLES: askPermission, getParameters(), getImage(), and doTileJob().
*/
// Start of Main Function
public static void main(String args[])
{
int MAXSIDE = 100;
Scanner scan = new Scanner(System.in);
char [][] buffer = new char [MAXSIDE][MAXSIDE];
String fileName = "";
int tilesAcross = 0;
int tilesDown = 0;
int imageHeight = 0;
int imageWidth = 0;
char userInput = 0;
System.out.println("Would you like to tile an image in a file?");
TileMap.askPermission(userInput);
TileMap.getParameters(fileName, tilesAcross, tilesDown);
TileMap.getImage(buffer, fileName, imageHeight, imageWidth);
TileMap.doTileJob(buffer, fileName, tilesDown, tilesAcross, imageHeight, imageWidth);
}
/*
FUNCTION NAME: askPermission ;
INPUT: none.
OUTPUT: a message to the user of this program.
PRECONDITIONS: output set to start on a new line.
POSTCONDITIONS: variable response has user's answer stored in it.
CALLERS: the main program
CALLES: None.
*/
static boolean askPermission(char response)
{
char y = 0;
Scanner scan = new Scanner(System.in);
System.out.println("If yes, type a 'y', else type 'n':");
response = scan.next().charAt(0);
boolean yes = (response == y);
if(yes = true)
return true;
else
return false;
}
/*
FUNCTION NAME getParameters ;
INPUT: the file name, number of tiles across and down.
OUTPUT: message "Getting Image".
PRECONDITIONS: the variable response has 'y' in it.
POSTCONDITIONS: variables set with the values entered by user.
CALLERS: the main program
CALLEES: none
*/
static void getParameters(String fileName, int tilesDown, int tilesAcross)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the file name: ");
fileName = scan.nextLine();
File file = new File(fileName);
System.out.println("Please enter the number of tiles across you want: ");
tilesAcross = scan.nextInt();
System.out.println("Please enter the number of tiles down you want: ");
tilesDown = scan.nextInt();
System.out.println("Getting Image...");
}
/*
FUNCTION NAME: getImage ;
INPUT:the file name and the height and width of the pattern to be made.
OUTPUT: the message "Getting Image".
PRECONDITIONS: array for image declared, the variables fileName,
imageHeight and imageWidth set with proper values.
POSTCONDITIONS: the image is stored in the array.
CALLERS: the main program
CALLEES: none
*/
static void getImage(char [][] buffer,
String fileName, int imageHeight, int imageWidth)
{
File file = new File(fileName);
try
{
Scanner fstream = new Scanner(file);
imageHeight = fstream.nextInt();
imageWidth = fstream.nextInt();
}
catch (Exception e)
{
System.err.println("Error: " + e.getMessage());
}
}
/*
FUNCTION NAME: doTileJob;
INPUT:the buffer with the image and the height and width of the
pattern to be made, and the user's input for tilesAcross, and tilesDown.
OUTPUT: the patterns structured according to users input.
PRECONDITIONS: All of the variables are set and pattern is stored in 'buffer'.
POSTCONDITIONS: Output displayed according to users input.
CALLERS: the main program
CALLEES: none
*/
// This function uses for loops to display the images. The inner most for loop prints one line of the picture.
static void doTileJob (char [] [] buffer , String fileName, int tilesDown, int tilesAcross, int imageHeight, int imageWidth)
{
buffer = new char[tilesDown][tilesAcross];
for(int i=0; i < imageHeight; i++)
{
for(int w = 0; w < imageWidth; w++)
{
System.out.println(fileName);
for(int t = 0; t < tilesAcross; t++)
{
for(int a = 0; a < tilesDown; a++)
{
System.out.println(fileName);
}
}
}
}
}
}