gpt4 book ai didi

Java:当用户输入名称时打开并输出文件

转载 作者:行者123 更新时间:2023-12-01 14:06:51 25 4
gpt4 key购买 nike

我有一个家庭作业,需要根据用户输入的内容输出一个文本 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);
}
}
}
}





}
}

最佳答案

不使用askPermission的返回值。您在方法中更新的字段是本地的,它们不会传回您的 main。

如果要访问类中的字段,则需要创建该类的实例。之后,您可以直接访问这些字段。像下面的代码这样的东西可能会更好地满足您的目的。

class TileMap    
{
static final int MAXSIDE = 100;
char [][] buffer = new char [MAXSIDE][MAXSIDE];
String fileName = "";
int tilesAcross = 0;
int tilesDown = 0;
int imageHeight = 0;
int imageWidth = 0;
char userInput = 0;

// Start of Main Function

public static void main(String args[])
{
TileMap tileMap = new TileMap();
tileMap.exec();
}

private void exec()
{
Scanner scan = new Scanner(System.in);
System.out.println("Would you like to tile an image in a file?");

if (askPermission()) {
getParameters();
getImage();
doTileJob();
}
}

// code without the static ...
}

关于Java:当用户输入名称时打开并输出文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18835589/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com