【发布时间】:2019-10-22 08:34:22
【问题描述】:
我需要存储我通过扫描仪从带有 while 循环的 .txt 文件中收集的数据。 .txt 中的行包含 x、y、z 坐标和 c 值。 我设法从 .txt 中拆分行并将它们转换为数字,但我无法在循环之外访问它们以从那里使用它们。我想在二维数组“z65”中使用“x-”和“y-”坐标和 c 值。
import java.io.File;
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Pattern;
import gdi.ct.CtImage ;
public class CtViewer {
public static void main(String[] args)
throws java.io.FileNotFoundException
{
int x;
int y;
int z;
int c;
int xValue;
int yValue;
int zValue;
int cValue;
int [] [] [] ct = new int [204] [204] [139];
for ( x = 0; x < 204; x++) {
for ( y = 0; y < 204; y++) {
for ( z = 0; z < 139; z++) {
ct[x][y][z] = -1000; }}}
System.out.println(ct [0] [0] [0]);
System.out.println(ct [0] [0] [1]);
File cTxt = new File("CT-Chest.txt");
Scanner readTxt = new Scanner(cTxt);
while(readTxt.hasNextLine()) {
String i = readTxt.nextLine();
String [] out = i.split(" ");
xValue = Integer.parseInt(out[0]);
yValue =Integer.parseInt(out[1]);
zValue= Integer.parseInt(out[2]);
cValue =Integer.parseInt(out[3]);
x =xValue;
y=yValue;
z=zValue;
c = cValue;
System.out.println(x+ "" +y+"" +z+ ""+c); //Works 'till here.
ct [x] [y][z] = c;
}
System.out.println(x+""+y);
int[] [] z65 = new int [x] [y];
CtImage cI = new CtImage(z65);
}
}
【问题讨论】:
-
x、y和z有哪些值?它们应该在0 <= x < 204、0 <= y < 204和0 <= x < 139之间才能做到ct [x] [y][z] = c; -
正确。这些值如您所描述。
标签: java arrays multidimensional-array while-loop