0

如何在下面的程序中找到一行中有多少个标记,然后我需要使用 Java 添加两个整数数组,因为我更熟悉 php,这对我来说有点挑战。我也从文本文件中获取输入。因此,这就是我到目前为止的程序。

输入文件将有多行,如 3736 17234 29823 84478 123745 2371 34237 823712

import java.io.*;
    import java.util.*;

    public class Sum {

    //must use a constant for the length of the array
    static int[] total= new int[25];
    static int[] val = new int[25];
    static int line= 0;
    static int word =0;
    public static void main(String[] args) throws FileNotFoundException {
    Scanner input = new Scanner(new File("Input.txt"));
//                  System.out.println("0");
    processFile(input);
    }

    public static void processFile(Scanner input) {
    //in this method you need to read your input file
    //. read one line at a time and call the method processLine to


    while (input.hasNextLine()) 
                {

                line++;
                String line = input.nextLine();
                Scanner lineScan = new Scanner(line);
                                //System.out.println("1");
                processLine(input);
                //System.out.println(line);
                }


    }

    public static void processLine(Scanner data) {
    //in this method you read tokens from line that has been passed to
    // this methd as the parameter. method transfer needs to be called to
    // transfer each token into an array of length DIGITS. Note that in a
    // line you might only have one token


        while(data.hasNext())
        {

          String x = data.next();
                        //System.out.println("2");
          transfer(x,val);
        }


    }

    public static void transfer(String data, int[] digits) {
    //This method transfer the string into array of integers value.

    int len = data.length();
    int n=24;
    for(int i=0;i<=n;i++)
        digits[i]=0;
                    //System.out.println("3");
        while(len>0)
        {
            //  System.out.println(data.charAt(len-1));
            char z=data.charAt(len-1);

            int d = Character.digit (z, 10); 

            digits[n]=d ;   
            len=len-1;
            n=n-1;          
        }
        for(int i=0;i<=n;i++)
            digits[i]=0;

        for(int i=0;i<25;i++)
        {
            //System.out.println(digits[i]);
        }
        System.out.println("\n");

            add(digits);

    }
    public static void add(int[] digits) {
            word++;
            if (word>1)
            {
                for(int i=0; i<= 4; i++)
                {
                    total= total[i]+digits[i];
                }


            }   
            if(word==0)
                total=digits;       
    }

    public static void print(int[] digits) {
    //For printing 


    }

    }
4

2 回答 2

2

使用 for 循环遍历 2 个数组并添加每个元素。

下面的快速示例代码:

       private int[] sumTwoArrays(int [] a, int [] b){
            if(a.length!=b.length){
               throw new IllegalArgumentException("Arrays are not of same length!");
            }
            int[] sum = new int[a.length];
            for(int i=0;i<a.length;i++){
               sum[i] = a[i]+b[i];
            }
            return sum;
       }

更新:在下面的评论之后,我添加了另一种关于如何仅在一个数组中添加元素的方法。

private void readFile(){
    long sum=0;
    //do your FILE I/O here
    //for each line you read into an array called input[] you call this method
    sum += sumArray(input);
    System.out.println(sum);
}

 private long sumArray(long [] a){
          long sum=0;
           for(int i=0;i<a.length;i++){
            sum += a[i];
           }
           return sum;
  }
于 2011-04-24T23:12:11.367 回答
1

你把这件事复杂化了,远远超出了必要的程度。首先,您可以删除多个函数引用,scanner 类实际上可以将您的文件解析为整数,并且可以读取多行 - 因此您不需要为每一行使用新的扫描仪。

我认为您可以简单地每个文件有一个 Scanner,然后不断使用 Scanner.nextInt() 函数来查找该文件中的下一个整数值。

现在,根据您对 add 的定义,您可以做两件事。

  1. 如果您尝试添加每个数组的相应元素,并附带第三个数组,其中 x[i] = y[i] + z[i],那么您只需要一个包含前面语句的 for 循环。当然要记住要替换变量的真实名称。
  2. 如果您尝试将两个数组中的所有整数相加以获得一个整数(或长整数),则可以使用两个 for 循环,一个接一个地不断将当前数组的当前元素添加到变量中。
于 2011-04-24T23:21:26.013 回答