프로그래밍에 익숙하지 않아 평신도의 말에 감사드립니다.파일의 내용을 2D 배열로 읽어들입니다.
9 개의 값 (3x3 배열)을 포함하는 파일 내용을 읽고 해당 내용을 해당 색인에 배치해야한다는 과제가 있습니다. 컨텐츠가 판독 된 후 예를 들어
,
차원 어레이의 내용은 ...
1.00 -2.00 3.00
4.00 1.00 -1.00
1.00 2.00 1.00
, 그들은 인쇄 할 필요가있다. 그러면 프로그램은 사용자에게 '4'와 같은 스칼라 승수를 묻습니다. 그런 다음 프로그램은 새 출력을 사용하여 새 배열을 인쇄해야합니다. 나는 현재이이
,
import java.io.*;
import java.util.*;
public class CS240Lab8a {
/**
* @param args the command line arguments
*/
static double [][] matrix = new double[3][3];
static Scanner input = new Scanner(System.in);
public static void main(String[] args) throws IOException
{
// Variables
String fileName = "ArrayValues.txt";
// print description
printDescription();
// read the file
readFile(fileName);
// print the matrix
printArray(fileName, matrix);
// multiply the matrix
multiplyArray(fileName, matrix);
}
// Program Description
public static void printDescription()
{
System.out.println("Your program is to read data from a file named ArrayValues.txt and store the values in a 2D 3 X 3 array. "
+ "\nNext your program is to ask the user for a scalar multiplier \n"
+ "which is then used to multiply each element of the 2D 3 X 3 array.\n\n");
}
// Read File
public static void readFile(String fileName) throws IOException
{
String line = "";
FileInputStream inputStream = new FileInputStream(fileName);
Scanner scanner = new Scanner(inputStream);
DataInputStream in = new DataInputStream(inputStream);
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
int lineCount = 0;
String[] numbers;
while ((line = bf.readLine()) != null)
{
numbers = line.split(" ");
for (int i = 0; i < 3; i++)
{
matrix[lineCount][i] = Double.parseDouble(numbers[i]);
}
lineCount++;
}
bf.close();
}
// Print Array
public static void printArray(String fileName, double [][] array)
{
System.out.println("The matrix is:");
for (int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
public static double multiplyArray(String fileName, double[][] matrix)
{
double multiply = 0;
System.out.println("Please enter the scalar multiplier to be used.");
multiply = input.nextDouble();
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
matrix[i][j]
return multiply;
}
} // end of class
나는 현재 제대로 배열을 인쇄 할 수 있습니다.
모든 색인 값에 상수 (사용자 입력)를 곱하는 가장 좋은 방법은 무엇입니까?
첫 번째 문제점은 매트릭스에 아무 것도 지정하지 않았다는 것입니다. 실제로 readFile 메서드 내에 matrix라는 새로운 2 차원 배열을 만듭니다. 두 번째 문제는 readFile 메소드에서 matrix [0] [0] = numbers; 루프에서 ... 행렬의 첫 번째 위치에만 값을 할당합니다. –
* "첫 번째 줄을 수락하지만 후에 실패합니다."* 어떻게 실패합니까? 구체적으로 말하십시오. BTW - 질문 있니? –
aleph_null - 'double'= (x, y) index와 같이 각 'double'을 인덱스에 저장하는 방법에 대한 제안이 있습니까? 또한 사전 소송을 제기 한 후 다음 색인으로 어떻게 넘어갈 수 있습니까? – fisherml