我必须创建一个从文本文件中读取数据的程序。我让它读取第一个第一个变量并将其放入等式中,但我不确定如何为其余变量设置它。
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
它编译正常,但它给了我Exception in thread "main" java.util.NoSuchElementException
答案 0 :(得分:0)
这看起来绝对是家庭作业。以下是一些指导,可以帮助您。您的代码非常冗余且冗长,正如我们所知,代码中的错误数量与代码行成正比。因此,代码行越少越好。 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();
}
}