Java数组不打印(正在使用BlueJ)

时间:2018-04-10 02:48:51

标签: java arrays boolean

我目前正在尝试完成一项需要使用java编程的大学任务。任务如下。

我必须通过输入文件进行排序以进行#34;钓鱼比赛"。该文件首先为每一行给出一个整数(在1-7之间),然后是一个实数。棘手的部分是数字尚未加起来或按顺序排列。这意味着我必须让程序读取整数,证明其所需的位置,然后将以下权重添加到该位置。

这里有一个关于文件外观的想法

3 36.2

6 27.8

7 10.3

我对如何完成整个任务有一般的想法,但是我被困在一个部分,即打印每个竞争者的总重量。

这是我到目前为止编写的代码。如果你能告诉我可能给我带来什么问题,我将不胜感激。 (它没有给出错误代码,而是BlueJ,我正在使用的软件,只是不断尝试运行程序)



import java.io.*;
import java.util.Scanner;
public class Project7
{
    public static void main(String[] args)
    throws FileNotFoundException
    {
        Scanner inF = new Scanner(new File("inputpro7.txt"));
        int [] arrayNum = new int[7];
        double [] arrayWeight = new double[7];
        int place = inF.nextInt();
        double weight = inF.nextDouble();
            for (int m = 0; m < 7; m++)
                {
                    arrayNum[m] = m + 1;
                }
            while (inF.hasNextLine())
                {
                    if (place == 1)
                        arrayWeight[0] += weight;
                    else if (place == 2)
                        arrayWeight[1] += weight;
                    else if (place == 3)
                        arrayWeight[2] += weight;
                    else if (place == 4)
                        arrayWeight[3] += weight;
                    else if (place == 5)
                        arrayWeight[4] += weight;
                    else if (place == 6)
                        arrayWeight[5] += weight;
                    else 
                        arrayWeight[6] += weight;    
                } 
            for (int k = 0; k < 7; k++) 
                System.out.printf("%6d %6.2f%n", arrayNum[k],                  
                arrayWeight[k]);
        inF.close();
    }
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

代码中的问题是在While循环中。您正在不断阅读输入文件的第一行。 While循环中没有读取nextline语句。即使您放置该语句(阅读下一行),您也需要对代码进行一些修改。以下是修改后的代码。

public class ExampleCode {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner inF = new Scanner(new File("Input.txt"));
        int[] arrayNum = new int[7];
        double[] arrayWeight = new double[7];
        int place;
        double weight;

        for (int m = 0; m < 7; m++) {
            arrayNum[m] = m + 1;
        }

        while (inF.hasNextLine()) {
            place = inF.nextInt();
            weight = inF.nextDouble();
            if (place == 1)
                arrayWeight[0] += weight;
            else if (place == 2)
                arrayWeight[1] += weight;
            else if (place == 3)
                arrayWeight[2] += weight;
            else if (place == 4)
                arrayWeight[3] += weight;
            else if (place == 5)
                arrayWeight[4] += weight;
            else if (place == 6)
                arrayWeight[5] += weight;
            else
                arrayWeight[6] += weight;
        }
        for (int k = 0; k < 7; k++)
            System.out.printf("%6d %6.2f%n", arrayNum[k], arrayWeight[k]);
        inF.close();
    }
}

解决此类问题的最佳方法“处理输入文件并根据类似的键值聚合输入”是使用HashMap。以下是可能的解决方案。

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.Scanner;

    public class ExampleCode {

        public static void main(String[] args) throws FileNotFoundException
        {
            Scanner inF = new Scanner(new File("Input.txt"));
            Map<String, Double> table = new HashMap<String, Double>(); 
            String line = "";
            String token[] = new String[2];

            //Process till end of file
            while (inF.hasNextLine())
            {
                //Read each lines
                line = inF.nextLine();
                //Split line based on separator i.e " " character
                //use these tokens as Key-Value pairs
                token = line.split(" ");

                //If table already has the entry add the value to same key entry
                if(table.containsKey(token[0])){
                    Double value = table.get(token[0]) + new Double(token[1]);
                    table.put(token[0], value);
                } else { //If key not found, insert the new entry
                    table.put(token[0], new Double(token[1]));
                }
            }

            for(Entry entry : table.entrySet()){
                System.out.println("Key:"+entry.getKey()+" == Value:"+entry.getValue());
            }
            //close the file 
            inF.close();
        }
    }

Sample Input file: 
2 9.25
4 8.5
1 1.5
2 4.215
5 1.4555
6 8.989
7 1
4 8.049
1 3.567

Output : 
Key:1 == Value:5.067
Key:2 == Value:13.465
Key:4 == Value:16.549
Key:5 == Value:1.4555
Key:6 == Value:8.989
Key:7 == Value:1.0