0

我有一个二维数组,我将其命名为查找表,如下所示:

123   256   19   52369564875214
147   307   51   12654987625425
136   94    14   65913210210871
301   818   23   74120032154894
215   103   81   54680045569748
...

...依此类推(数组大小为 700x4)

这项工作的目的是要求用户输入三个数字,比如 a、b 和 c。然后,此代码应查看表(上面的数组)并返回相应的值 d,其中 d 是从最后一列中提取的。例如,如果用户输入 (147,307,51),那么代码应该给用户 12654987625425。实际上作为函数 f(a,b,c)=f(147,307,51)=12654987625425。

package arrayread;

import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;

public class ArrayRead {
    //  
    public static void main(String [] args)
    {
        ArrayRead ar = new ArrayRead();
        ar.readArray("C:/Users/Desktop/tsp2.txt",",");
        String arr[][] = ar.getArray();
        int rows = ar.getRows();
        int cols = ar.getCols();

        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < cols; c++) {
                System.out.print(arr[r][c]+" ");
            }
            System.out.println();
        }   
    }

    private String arr[][];
    private int rows, cols;

    String[][] getArray()
    {
        return arr;
    }

    int getRows()
    {
        return rows;
    }

    int getCols()
    {
        return cols;
    }

    // Read using DELIMITER TAB
    void readArray(String arrayName)
    {
        rows = 0;
        cols = 0;
        int r, c, numElements;
        String ipString, splitString[];
        ArrayList <String>tmpArr = new ArrayList<>();

        try
        {
            Scanner in = new Scanner(new FileReader(arrayName));
            while(in.hasNext())
            {
                ipString = in.nextLine();
                splitString = ipString.split("\t");
                if (rows==0)
                {
                    cols = splitString.length;
                }
                for (c=0;c<cols;c++)
                {
                    tmpArr.add(splitString[c]);
                }
                rows++;
            }
            in.close();

            arr = new String[rows][cols];
            numElements = 0;
            for (r=0; r<rows; r++)
            {
                for (c=0; c<cols; c++)
                {
                    arr[r][c] = tmpArr.get(numElements);
                    numElements++;
                }
            }
        }
        catch (Exception e)
        {
            System.out.println("Error occurred!");
        }
    }

    // Read using DELIMITER regex
    void readArray(String arrayName, String regex)
    {
        rows = 0;
        cols = 0;
        int r, c, numElements;
        String ipString, splitString[];
        ArrayList <String>tmpArr = new ArrayList<>();

        try
        {
            Scanner in = new Scanner(new FileReader(arrayName));
            while(in.hasNext())
            {
                ipString = in.nextLine();
                splitString = ipString.split(regex);
                if (rows==0)
                {
                    cols = splitString.length;
                }
                for (c=0;c<cols;c++)
                {
                    tmpArr.add(splitString[c]);
                }
                rows++;
            }
            in.close();

            arr = new String[rows][cols];
            numElements = 0;
            for (r=0; r<rows; r++)
            {
                for (c=0; c<cols; c++)
                {
                    arr[r][c] = tmpArr.get(numElements);
                    numElements++;
                }
            }
        }
        catch (Exception e)
        {
            System.out.println("Error occurred!");
        }
    }
}
4

1 回答 1

0

所以本质上,如果我理解正确,你得到 arr[x][0,1,2] 并且你想返回 arr[x][3]

如果是这种情况,你要做的是:

for(int i = 0; i < 700; i++){
    if(arr[i][0] == entry1 && arr[i][1] == entry2 && arr[i][2] == entry3)
        return arr[i][3];
}

编辑:要从命令行读取,您需要执行以下操作:

//  open up standard input
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try{
    int entry1 = Integer.parseInt(br.readLine());
    int entry2 = Integer.parseInt(br.readLine());
    int entry3 = Integer.parseInt(br.readLine());
}catch(IOException ioe){
    // handle exception
}

PS:抱歉耽搁了

于 2013-10-24T18:46:02.433 回答