将循环中的总计存储为双精度数

时间:2013-12-02 16:47:16

标签: java arrays input double

我目前正在寻找一种方法来存储总计来自计算从循环中计算出的输入文件中获取的值。我试图找出一种方法将总计存储为双打,这样我就可以从控制台获取用户输入并将总数与输入进行比较。这是我到目前为止的代码;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Assignment3 {

static Scanner console = new Scanner(System.in);

public static void main(String[] args) throws FileNotFoundException {

    Scanner input = new Scanner(new FileReader("AssistantHoursAndRates.txt"));

    double UnitRM;
    System.out.println("Enter recommended maximum staff cost of Unit 1");
        UnitRM = console.nextDouble ();
            System.out.println("Recommended maximum staff cost of Unit1 = "+UnitRM);

            System.out.println("         ");

    int unit = 1;
    while (input.hasNextLine()) {
        String line = input.nextLine();
        System.out.println(line);

        double total = 0;

        int assistants = input.nextInt();
        System.out.println("Number of Assistants " + assistants);
        System.out.println("Hours  Rate");
        System.out.println("------------");
        for (int i = 0; i < assistants; i++) {
            int hours = input.nextInt();
            System.out.print(hours + "     ");
            double rate = input.nextDouble();
            System.out.println(rate);               
            total += (hours * rate);
        }


System.out.println("Total cost of Unit " + unit + " is " + total);
System.out.println();
    unit++;



        if (input.hasNextLine()) {
            input.nextLine();
            input.next();





          }

      } 
  }

}

如果需要,这是输入文件;

Unit One
4
32 8
38 6
38 6
16 7

Unit Two
0

Unit Three
2
36 7
36 7

Unit Four
6
32 6.5
32 6.5
36 6.5
36 6.5
38 6.5
38 6.5

Unit Five
4
32 6.5
32 8
32 7
32 8

Unit Six
5
38 7
30 6.5
24 8
24 8
24 8

Unit Seven
0

Unit Eight
1
40 12

Unit Nine
5
24 8
24 6.5
30 6.5
24 7
32 7

我的一位朋友建议使用数组,任何帮助都会受到赞赏。

更新

目前的代码;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Assignment3 {

static Scanner console = new Scanner(System.in);

public static void main(String[] args) throws FileNotFoundException {

    Scanner input = new Scanner(new FileReader("AssistantHoursAndRates.txt"));

    double UnitRM1;
        System.out.println("Enter recommended maximum staff cost of Unit 1");
            UnitRM1 = console.nextDouble ();
                System.out.println("Recommended maximum staff cost of Unit1 = "+UnitRM1);

                System.out.printf("%10s\n", " ");

    double UnitRM2;
            System.out.println("Enter recommended maximum staff cost of Unit 2");
                UnitRM2 = console.nextDouble ();
                    System.out.println("Recommended maximum staff cost of Unit2 = "+UnitRM2);

                    System.out.printf("%10s\n", " ");

    double UnitRM3;
            System.out.println("Enter recommended maximum staff cost of Unit 3");       
                UnitRM3 = console.nextDouble ();        
                    System.out.println("Recommended maximum staff cost of Unit3 = "+UnitRM3);   

                    System.out.printf("%10s\n", " ");

    double UnitRM4;
            System.out.println("Enter recommended maximum staff cost of Unit 4");
                UnitRM4 = console.nextDouble ();
                    System.out.println("Recommended maximum staff cost of Unit4 = "+UnitRM4);   

                    System.out.printf("%10s\n", " ");

     double UnitRM5;            
             System.out.println("Enter recommended maximum staff cost of Unit 5");      
                UnitRM5 = console.nextDouble ();        
                    System.out.println("Recommended maximum staff cost of Unit5 = "+UnitRM5);   

                    System.out.printf("%10s\n", " ");

     double UnitRM6;            
             System.out.println("Enter recommended maximum staff cost of Unit 6");              
                UnitRM6 = console.nextDouble ();       
                    System.out.println("Recommended maximum staff cost of Unit6 = "+UnitRM6);

                    System.out.printf("%10s\n", " ");

     double UnitRM7;
             System.out.println("Enter recommended maximum staff cost of Unit 7");
                UnitRM7 = console.nextDouble ();
                    System.out.println("Recommended maximum staff cost of Unit7 = "+UnitRM7);

                    System.out.printf("%10s\n", " ");

     double UnitRM8;            
             System.out.println("Enter recommended maximum staff cost of Unit 8");      
                UnitRM8 = console.nextDouble ();        
                    System.out.println("Recommended maximum staff cost of Unit8 = "+UnitRM8);   

                    System.out.printf("%10s\n", " ");

     double UnitRM9;                
            System.out.println("Enter recommended maximum staff cost of Unit 9");               
               UnitRM9 = console.nextDouble ();     
                   System.out.println("Recommended maximum staff cost of Unit9 = "+UnitRM9);




    double[] totals = new double[9];        
    int unit = 1;
    while (input.hasNextLine()) {
        String line = input.nextLine();
        System.out.println(line);

        double total = 0;

        int assistants = input.nextInt();
        System.out.println("Number of Assistants " + assistants);
        System.out.println("Hours  Rate");
        System.out.println("------------");
        for (int i = 0; i < assistants; i++) {
            int hours = input.nextInt();
            System.out.print(hours + "     ");
            double rate = input.nextDouble();
            System.out.println(rate);               
            total += (hours * rate);
        }


System.out.println("Total cost of Unit " + unit + " is " + total);
System.out.println();   
totals[unit - 1] = total;
    unit++;



        if (input.hasNextLine()) {
            input.nextLine();
            input.next();



        } 


    }


        System.out.println("Comparisons are as follows;");       

    }


}

给出错误;

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at Assignment3.main(Assignment3.java:108)

2 个答案:

答案 0 :(得分:0)

创建一个double数组来保存总计

double[] totals = new double[9];  // all the totals are stored here
unit = 1;
...

while(input.hasNextLine()){
    ...
    ...

    totals[unit - 1] = total;
    unit++;
}

答案 1 :(得分:0)

我个人认为你的代码看起来不错,但有一行我注意到在某些情况下会变得混乱。

System.out.println("         ");

这是非常低效的,考虑到你必须物理地隔离一切。例如,如果您的数据要更改,您的间距最终将会关闭。更不用说你必须经常出局:D。有一种更好的方法:

System.out.printf("%9s\n", " ");

这将打印9个空格,就好像你已经击中空格键9次一样。

希望它有所帮助!