Java 如何将下一组变量分配给方程?

Java 如何将下一组变量分配给方程?,java,math,Java,Math,我必须创建一个从文本文件读取数据的程序。我让它读取第一个变量,并将其放入方程中,但我不确定如何设置其余变量 public static void main(String[] args) throws Exception { // Scanner stdIn = new Scanner(System.in); Scanner stdIn = new Scanner(new File("energyProduced.txt")); stdIn.nextDouble();

我必须创建一个从文本文件读取数据的程序。我让它读取第一个变量,并将其放入方程中,但我不确定如何设置其余变量

 public static void main(String[] args) throws Exception
 {
//   Scanner stdIn = new Scanner(System.in);
   Scanner stdIn = new Scanner(new File("energyProduced.txt"));
        stdIn.nextDouble();
        double energy1;
        double energy2;
        double energy3;
        double energy4;
        double energy5;
        double energy6;
        double energy7;
        energy1 = stdIn.nextDouble();
        energy2 = stdIn.nextDouble();
        energy3 = stdIn.nextDouble();
        energy4 = stdIn.nextDouble();
        energy5 = stdIn.nextDouble();
        energy6 = stdIn.nextDouble();
        energy7 = stdIn.nextDouble();
   int systemsCost;  //Total Systems Cost
   double ttlEnergy; //Total energy produced in a week
   double savingsWeek; //Total savings for one week
   double savingsDay;  //Total savings for one day
   double recoupDay;  //Days to recoup
   double recoupYear; //Years to recoup
   final double electricCost = 0.085;
//

   ttlEnergy = (energy1 * 7);
   savingsWeek = (ttlEnergy * electricCost);
   savingsDay = (savingsWeek / 7);
   systemsCost = (int) (savingsWeek * energy1);
   recoupDay = (int) (systemsCost / savingsDay);
   recoupYear = (int) (recoupDay / 365);


//
    System.out.println("Total System Cost = $" + systemsCost );
    System.out.println("Total Energy Produced in one week" + (ttlEnergy) + "Kwh");
    System.out.println("Total Savings for one week =" + savingsWeek);
    System.out.println("Total Savings for one day =" + savingsDay);
    System.out.println("Days to recoup cost(truncated)" + recoupDay);
    System.out.println("Years to recoup cost(truncated)" + recoupYear);
    System.out.println();   
它只读能量1,不读其余的2-7,我该怎么做才能让它读剩下的? 编辑 .txt文件是一个数字列表: 23(只有这一条) 26 75 98 45 等等

EDIT2

我确实重写了一点

 public static void main(String[] args) throws Exception
 {
//   Scanner stdIn = new Scanner(System.in);
   Scanner stdIn = new Scanner(new File("energyProduced.txt"));
        stdIn.nextDouble();
        double energy1;
        double energy2;
        double energy3;
        double energy4;
        double energy5;
        double energy6;
        double energy7;
        energy1 = stdIn.nextDouble();
        energy2 = stdIn.nextDouble();
        energy3 = stdIn.nextDouble();
        energy4 = stdIn.nextDouble();
        energy5 = stdIn.nextDouble();
        energy6 = stdIn.nextDouble();
        energy7 = stdIn.nextDouble();
   int systemsCost;  //Total Systems Cost
   double ttlEnergy; //Total energy produced in a week
   double savingsWeek; //Total savings for one week
   double savingsDay;  //Total savings for one day
   double recoupDay;  //Days to recoup
   double recoupYear; //Years to recoup
   int systemsCost2;  //Total Systems Cost
   double ttlEnergy2; //Total energy produced in a week
   double savingsWeek2; //Total savings for one week
   double savingsDay2;  //Total savings for one day
   double recoupDay2;  //Days to recoup
   double recoupYear2; //Years to recoup
   int systemsCost3;  //Total Systems Cost
   double ttlEnergy3; //Total energy produced in a week
   double savingsWeek3; //Total savings for one week
   double savingsDay3;  //Total savings for one day
   double recoupDay3;  //Days to recoup
   double recoupYear3; //Years to recoup
   int systemsCost4;  //Total Systems Cost
   double ttlEnergy4; //Total energy produced in a week
   double savingsWeek4; //Total savings for one week
   double savingsDay4;  //Total savings for one day
   double recoupDay4;  //Days to recoup
   double recoupYear4; //Years to recoup
   int systemsCost5;  //Total Systems Cost
   double ttlEnergy5; //Total energy produced in a week
   double savingsWeek5; //Total savings for one week
   double savingsDay5;  //Total savings for one day
   double recoupDay5;  //Days to recoup
   double recoupYear5; //Years to recoup
   int systemsCost6;  //Total Systems Cost
   double ttlEnergy6; //Total energy produced in a week
   double savingsWeek6; //Total savings for one week
   double savingsDay6;  //Total savings for one day
   double recoupDay6;  //Days to recoup
   double recoupYear6; //Years to recoup
   int systemsCost7;  //Total Systems Cost
   double ttlEnergy7; //Total energy produced in a week
   double savingsWeek7; //Total savings for one week
   double savingsDay7;  //Total savings for one day
   double recoupDay7;  //Days to recoup
   double recoupYear7; //Years to recoup
   final double electricCost = 0.085;


   ttlEnergy = (energy1 * 7);
   savingsWeek = (ttlEnergy * electricCost);
   savingsDay = (savingsWeek / 7);
   systemsCost = (int) (savingsWeek * energy1);
   recoupDay = (int) (systemsCost / savingsDay);
   recoupYear = (int) (recoupDay / 365);
   ttlEnergy2 = (energy2 * 7);
   savingsWeek2 = (ttlEnergy * electricCost);
   savingsDay2 = (savingsWeek / 7);
   systemsCost2 = (int) (savingsWeek * energy2);
   recoupDay2 = (int) (systemsCost / savingsDay);
   recoupYear2 = (int) (recoupDay / 365);
   ttlEnergy3 = (energy3 * 7);
   savingsWeek3 = (ttlEnergy * electricCost);
   savingsDay3 = (savingsWeek / 7);
   systemsCost3 = (int) (savingsWeek * energy3);
   recoupDay3 = (int) (systemsCost / savingsDay);
   recoupYear3 = (int) (recoupDay / 365);
   ttlEnergy4 = (energy4 * 7);
   savingsWeek4 = (ttlEnergy * electricCost);
   savingsDay4 = (savingsWeek / 7);
   systemsCost4 = (int) (savingsWeek * energy4);
   recoupDay4 = (int) (systemsCost / savingsDay);
   recoupYear4 = (int) (recoupDay / 365);
   ttlEnergy5 = (energy5 * 7);
   savingsWeek5 = (ttlEnergy * electricCost);
   savingsDay5 = (savingsWeek / 7);
   systemsCost5 = (int) (savingsWeek * energy5);
   recoupDay5 = (int) (systemsCost / savingsDay);
   recoupYear5 = (int) (recoupDay / 365);
   ttlEnergy6 = (energy6 * 7);
   savingsWeek6 = (ttlEnergy * electricCost);
   savingsDay6 = (savingsWeek / 7);
   systemsCost6 = (int) (savingsWeek * energy6);
   recoupDay6 = (int) (systemsCost / savingsDay);
   recoupYear6 = (int) (recoupDay / 365);
   ttlEnergy7 = (energy7 * 7);
   savingsWeek7 = (ttlEnergy * electricCost);
   savingsDay7 = (savingsWeek / 7);
   systemsCost7 = (int) (savingsWeek * energy7);
   recoupDay7 = (int) (systemsCost / savingsDay);
   recoupYear7 = (int) (recoupDay / 365);


    System.out.println("Total System Cost = $" + systemsCost );
    System.out.println("Total Energy Produced in one week" + (ttlEnergy) + "Kwh");
    System.out.println("Total Savings for one week =" + savingsWeek);
    System.out.println("Total Savings for one day =" + savingsDay);
    System.out.println("Days to recoup cost(truncated)" + recoupDay);
    System.out.println("Years to recoup cost(truncated)" + recoupYear);
    System.out.println();   
    System.out.println("Total System Cost = $" + systemsCost2 );
    System.out.println("Total Energy Produced in one week" + (ttlEnergy2) + "Kwh");
    System.out.println("Total Savings for one week =" + savingsWeek2);
    System.out.println("Total Savings for one day =" + savingsDay2);
    System.out.println("Days to recoup cost(truncated)" + recoupDay2);
    System.out.println("Years to recoup cost(truncated)" + recoupYear2);
    System.out.println();   
    System.out.println("Total System Cost = $" + systemsCost3 );
    System.out.println("Total Energy Produced in one week" + (ttlEnergy3) + "Kwh");
    System.out.println("Total Savings for one week =" + savingsWeek3);
    System.out.println("Total Savings for one day =" + savingsDay3);
    System.out.println("Days to recoup cost(truncated)" + recoupDay3);
    System.out.println("Years to recoup cost(truncated)" + recoupYear3);
    System.out.println();
    System.out.println("Total System Cost = $" + systemsCost4 );
    System.out.println("Total Energy Produced in one week" + (ttlEnergy4) + "Kwh");
    System.out.println("Total Savings for one week =" + savingsWeek4);
    System.out.println("Total Savings for one day =" + savingsDay4);
    System.out.println("Days to recoup cost(truncated)" + recoupDay4);
    System.out.println("Years to recoup cost(truncated)" + recoupYear4);
    System.out.println();
    System.out.println("Total System Cost = $" + systemsCost5 );
    System.out.println("Total Energy Produced in one week" + (ttlEnergy5) + "Kwh");
    System.out.println("Total Savings for one week =" + savingsWeek5);
    System.out.println("Total Savings for one day =" + savingsDay5);
    System.out.println("Days to recoup cost(truncated)" + recoupDay5);
    System.out.println("Years to recoup cost(truncated)" + recoupYear5);
    System.out.println();   
    System.out.println("Total System Cost = $" + systemsCost6 );
    System.out.println("Total Energy Produced in one week" + (ttlEnergy6) + "Kwh");
    System.out.println("Total Savings for one week =" + savingsWeek6);
    System.out.println("Total Savings for one day =" + savingsDay6);
    System.out.println("Days to recoup cost(truncated)" + recoupDay6);
    System.out.println("Years to recoup cost(truncated)" + recoupYear6);
    System.out.println();   
    System.out.println("Total System Cost = $" + systemsCost7 );
    System.out.println("Total Energy Produced in one week" + (ttlEnergy7) + "Kwh");
    System.out.println("Total Savings for one week =" + savingsWeek7);
    System.out.println("Total Savings for one day =" + savingsDay7);
    System.out.println("Days to recoup cost(truncated)" + recoupDay7);
    System.out.println("Years to recoup cost(truncated)" + recoupYear7);
    System.out.println();   

 } // end main

它编译得很好,但是它在线程“main”java.util.NoSuchElementException中给了我一个
异常
这看起来确实是一个家庭作业。这里有一些建议可以帮助你在路上。您的代码是非常冗余和冗长的,正如我们所知,代码中的bug数量与代码行数成正比。因此,代码行越少越好。java中有一些数据结构,例如列表和数组,可以帮助您完成我们的任务,因此您不必编写要使用的每个变量。我不是一名高级开发人员,也不是一名专家,但这是一名中级开发人员解决这个问题的方法*注意,我没有机会编译这个,因为我使用的是记事本++并且没有我的另一台计算机

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

     // i like to underscore my static final variables
     // so i know when i use them
    static final double _electricCost = 0.085;

    int systemsCost = 0;

    List<Double> energyProductList = new ArrayList();

    Scanner fileScanner = new Scanner(new File("energyProduced.txt"));

    //to prevent the no such element error you need to make sure
    // there is an element to be had with

    while(fileScanner.hasNext())
    {
        //your values looked like ints so we'll use that
        int energyProduced = fileScanner.nextInt();
        energyProductList.add(energyProduced);
    }

    // now that we've grabbed all our energy values we can
    // iterate them and do the calculations we need

    // we will create some lists to contain our summary data

    List<Integer> weeklyEnergy = new ArrayList();
    List<Double> weeklySavings = new ArrayList();
    List<Double> savingsPerDay = new ArrayList();
    List<Double> systemsCost = new ArrayList();
    List<Integer> recoupDay = new ArrayList();
    List<Integer> recoupYear  = new ArrayList();


    for(int energy:energyProductList)
    {
        int tempWeeklyEnergy = energy * 7;
        double tempWeeklySavings = tempWeeklyEnergy * _electricCost;
        double tempSavingsPerDay = tempWeeklySavings / 7;
        double tempSystemsCost = tempWeeklySavings * energy;
        int tempRecoupDay = (int)(tempSystemsCost / tempSavingsPerDay);
        int tempRecoupYear = (int) (recoupDay / 365);


        //here we are putting all our calculated values in their corresponding
        // containers
        weeklyEnergy.add(tempWeeklyEnergy);
        weeklySavings.add(tempWeeklySavings);
        savingsPerDay.add(tempSavingsPerDay);
        systemsCost.add(tempSystemsCost);
        recoupDay.add(tempRecoupDay);
        recoupYear.add(tempRecoupYear);
    }

    //now that we have our values in nice lists so the length of them can be as big
    // or as small as they want we can print

    int max = energyProductList.size();
    int counter = 0;
    while(counter <= max)
    {   
    System.out.println("Energy -> " + counter)
    System.out.println("Total System Cost = $" + systemsCost.get(counter) );
    System.out.println("Total Energy Produced in one week" + (weeklyEnergy.get(counter)) + "Kwh");
    System.out.println("Total Savings for one week =" + weeklySavings.get(counter));
    System.out.println("Total Savings for one day =" + savingsPerDay.get(counter));
    System.out.println("Days to recoup cost(truncated)" + recoupDay.get(counter));
    System.out.println("Years to recoup cost(truncated)" + recoupYear.get(counter));
    System.out.println();   
    }




 }
publicstaticvoidmain(字符串[]args)引发异常
{
//我喜欢强调我的静态最终变量
//所以我知道我什么时候用它们
静态最终双电成本=0.085;
int systemsCost=0;
List energyProductList=新的ArrayList();
Scanner fileScanner=new Scanner(新文件(“energyProduced.txt”);
//要防止无此类元素错误,您需要确保
//有一个因素需要考虑
while(fileScanner.hasNext())
{
//您的值看起来像int,所以我们将使用它
int energyProduced=fileScanner.nextInt();
energyProductList.add(energyProduced);
}
//现在我们已经掌握了所有的能量值
//迭代它们并进行我们需要的计算
//我们将创建一些列表来包含我们的摘要数据
List weeklyEnergy=new ArrayList();
List weeklySavings=new ArrayList();
List savingsPerDay=new ArrayList();
List systemsCost=new ArrayList();
List REQUEPDAY=新建ArrayList();
List REQUEPYEAR=new ArrayList();
for(int-energy:energyProductList)
{
int tempWeeklyEnergy=能量*7;
双温周节能=温周节能*\u电气成本;
双临时储蓄日=临时每周储蓄/7;
双tempSystemsCost=tempWeeklySavings*能量;
int temprecompday=(int)(tempSystemsCost/tempSavingsPerDay);
int temprecurypyear=(int)(补偿日期/365);
//在这里,我们将所有的计算值放入相应的
//容器
weeklyEnergy.add(tempWeeklyEnergy);
添加(tempWeeklySavings);
savingsPerDay.add(临时savingsPerDay);
systemsCost.add(tempSystemsCost);
REQUEPDAY.add(临时REQUEPDAY);
add(temprecurypyear);
}
//现在,我们已经在漂亮的列表中列出了我们的值,所以它们的长度可以一样大
//或者他们想要多小我们就可以打印
int max=energyProductList.size();
int计数器=0;

虽然(counter这看起来绝对是一个家庭作业。这里有几点建议可以帮助你。你的代码是非常冗余和冗长的,正如我们所知,代码中的错误数量与代码行数成正比。因此,代码行数越少越好。java中有一些数据结构,如列表和数组,可用于我不是高级开发人员,也不是自称专家,但这是中级开发人员解决这个问题的方法。*请注意,我没有机会编译这个,因为我使用的是notepad++并且没有其他计算机

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

     // i like to underscore my static final variables
     // so i know when i use them
    static final double _electricCost = 0.085;

    int systemsCost = 0;

    List<Double> energyProductList = new ArrayList();

    Scanner fileScanner = new Scanner(new File("energyProduced.txt"));

    //to prevent the no such element error you need to make sure
    // there is an element to be had with

    while(fileScanner.hasNext())
    {
        //your values looked like ints so we'll use that
        int energyProduced = fileScanner.nextInt();
        energyProductList.add(energyProduced);
    }

    // now that we've grabbed all our energy values we can
    // iterate them and do the calculations we need

    // we will create some lists to contain our summary data

    List<Integer> weeklyEnergy = new ArrayList();
    List<Double> weeklySavings = new ArrayList();
    List<Double> savingsPerDay = new ArrayList();
    List<Double> systemsCost = new ArrayList();
    List<Integer> recoupDay = new ArrayList();
    List<Integer> recoupYear  = new ArrayList();


    for(int energy:energyProductList)
    {
        int tempWeeklyEnergy = energy * 7;
        double tempWeeklySavings = tempWeeklyEnergy * _electricCost;
        double tempSavingsPerDay = tempWeeklySavings / 7;
        double tempSystemsCost = tempWeeklySavings * energy;
        int tempRecoupDay = (int)(tempSystemsCost / tempSavingsPerDay);
        int tempRecoupYear = (int) (recoupDay / 365);


        //here we are putting all our calculated values in their corresponding
        // containers
        weeklyEnergy.add(tempWeeklyEnergy);
        weeklySavings.add(tempWeeklySavings);
        savingsPerDay.add(tempSavingsPerDay);
        systemsCost.add(tempSystemsCost);
        recoupDay.add(tempRecoupDay);
        recoupYear.add(tempRecoupYear);
    }

    //now that we have our values in nice lists so the length of them can be as big
    // or as small as they want we can print

    int max = energyProductList.size();
    int counter = 0;
    while(counter <= max)
    {   
    System.out.println("Energy -> " + counter)
    System.out.println("Total System Cost = $" + systemsCost.get(counter) );
    System.out.println("Total Energy Produced in one week" + (weeklyEnergy.get(counter)) + "Kwh");
    System.out.println("Total Savings for one week =" + weeklySavings.get(counter));
    System.out.println("Total Savings for one day =" + savingsPerDay.get(counter));
    System.out.println("Days to recoup cost(truncated)" + recoupDay.get(counter));
    System.out.println("Years to recoup cost(truncated)" + recoupYear.get(counter));
    System.out.println();   
    }




 }
publicstaticvoidmain(字符串[]args)引发异常
{
//我喜欢强调我的静态最终变量
//所以我知道我什么时候用它们
静态最终双电成本=0.085;
int systemsCost=0;
List energyProductList=新的ArrayList();
Scanner fileScanner=new Scanner(新文件(“energyProduced.txt”);
//要防止无此类元素错误,您需要确保
//有一个因素需要考虑
while(fileScanner.hasNext())
{
//您的值看起来像int,所以我们将使用它
int energyProduced=fileScanner.nextInt();
energyProductList.add(energyProduced);
}
//现在我们已经掌握了所有的能量值
//迭代它们并进行我们需要的计算
//我们将创建一些列表来包含我们的摘要数据
List weeklyEnergy=new ArrayList();
List weeklySavings=new ArrayList();
List savingsPerDay=new ArrayList();
List systemsCost=new ArrayList();
List REQUEPDAY=新建ArrayList();
List REQUEPYEAR=new ArrayList();
for(int-energy:energyProductList)
{
int tempWeeklyEnergy=能量*7;
双温周节能=温周节能*\u电气成本;
双临时储蓄日=临时每周储蓄/7;
双tempSystemsCost=tempWeeklySavings*能量;
int temprecompday=(int)(tempSystemsCost/tempSavingsPerDay);
int temprecurypyear=(int)(补偿日期/365);
//在这里,我们将所有的计算值放入相应的
//容器
weeklyEnergy.add(tempWeeklyEnergy);
添加(tempWeeklySavings);
savingsPerDay.add(临时savingsPerDay);
systemsCost.add(tempSystemsCost);
REQUEPDAY.add(临时REQUEPDAY);
add(temprecurypyear);
}
//现在,我们已经在漂亮的列表中列出了我们的值,所以它们的长度可以一样大
//或者他们想要多小我们就可以打印
int max=energyProductList.s