我已经为我的java编程课程设置了一个任务,我已经达到了一个我真的无法完成下一步的工作。我想知道是否有人可以帮助我这个代码。作业如下:
存在一个文件,其中包含一年中每个月的总降雨量,每行一个双倍值。写一个程序:
到目前为止,我已经完成了前两个步骤,我几乎已经完成了第三步的代码。
使用的数据文件看起来像这样:
1.80 2.70 3.75 4.40 5.20 6.15 7.30 8.45 9.60 10.90 11.85 12.100
我在这里写的是:
1.80
2.70等意味着'1'将是1月,'2'将是2月等
完全停止后的数字是降雨量。所以我需要计算每个句号右侧所有数字的总和。
到目前为止我的代码是这样的:
import java.util.*;
import java.io.*;
class Assignment5{
public static void main(String[] args)throws Exception{
String data;
ArrayList<Double> store = new ArrayList<Double>();
Scanner scanner = new Scanner(System.in);
System.out.println(" ");
System.out.println("Please enter the name of a file with .txt at the end.");
System.out.println(" ");
data = scanner.nextLine();
File inputFile = new File(data);
Scanner reader = new Scanner(inputFile);
while(reader.hasNext()){
store.add(reader.nextDouble());
}
calculateSum(store);
store.clear();
}
private static void calculateSum(ArrayList<Double> ArrayList){
double sum = 0;
double avg = 0;
double total = 0;
double totalrainfall = 0;
Iterator<Double> iterator = ArrayList.iterator();
while(iterator.hasNext()){
sum += iterator.next();
}
total = ArrayList.size();
avg = (sum / total);
System.out.println(" ");
System.out.println("The total rainfall is " + totalrainfall);
}
我坚持的部分是计算总降雨量。数据是一个double值,所以我需要计算数据文件中完全停止后的所有整数。我无法弄清楚如何将它放入我的代码中。感谢您提前帮助!
答案 0 :(得分:2)
我很想读取文件的每一行作为字符串,将其拆分为“。”字符,并将得到的子字符串解析为整数,以获取该月份的月份数和降雨量。
我的Java有点生疏,但我会用:
import java.util.;
import java.io.;
class Assignment5
{
public static void main(String[] args) throws Exception
{
String data;
int[] store = new int[12];
Scanner scanner = new Scanner(System.in);
System.out.println(" ");
System.out.println("Please enter the name of a file with .txt at the end.");
System.out.println(" ");
data = scanner.nextLine();
File inputFile = new File(data);
Scanner reader = new Scanner(inputFile);
while(reader.hasNext())
{
String currentLine = scanner.nextLine();
String[] currentMonthDetails = currentLine.split(".");
int currentMonth = Integer.parseInt(currentMonthDetails[0]);
int currentRainfall = Integer.parseInt(currentMonthDetails[1]);
store[currentMonth] = currentRainfall;
}
calculateSum(store);
store = null;
}
private static void calculateSum(int[] aRainfallData)
{
int avgRainfall = 0;
int numberOfMonths = 0;
int totalRainfall = 0;
numberOfMonths = aRainfallData.length;
for (int index=0; index < numberOfMonths ; index++)
{
totalRainfall += aRainfallData[index];
}
avgRainfall = (totalRainfall / numberOfMonths );
System.out.println(" ");
System.out.println("The total rainfall is " + totalRainfall );
}
}
这假设文件只包含12个月的数据,这可能不是一个有效的假设。
答案 1 :(得分:2)
由于这是一项任务,请允许我对你对它的解释有一些想法(如果你的导师和我的一样严格,每次改变都会导致减去标记):
您的文本文件的格式(在我看来)与文字说明不符,我将其解释为
的描述80.0
70.0
75.0
// ...
以80.0,70.0,75.0 ......为几个月的降雨量。
此外,它说您应该使用“数组” - 我认为这意味着double[]
,您使用的是ArrayList
。
答案 2 :(得分:0)
数组列表中的每个其他元素都是一个月的降雨量,因此请使用布尔值来切换true false以汇总所有这些值。
double sum = 0;
boolean toggle = false;
for(Object d: arrayList) {
[...]
if(toggle) {
sum += (Double)d;
toggle = !toggle;
}
}
答案 3 :(得分:0)
您的数据采用该格式有点令人讨厌。扫描仪很好用且易于使用,但很难将数字从双重格式中恢复。
OK ...如果所有数据在小数点后都有2位数,则可以通过Math.round(Math.floor(x))将月份拉出为整数,其中x是您的1.80数字。
然后你可以从数字中减去月份并乘以100并再次应用Math.round()来获得降雨量。
答案 4 :(得分:0)
我建议如下:
//Don't use * imports it is bad practice
import java.util.*;
import java.io.*;
class Assignment5{
public static void main(String[] args)throws Exception{
String data;
ArrayList<Double> store = new ArrayList<Double>();
Scanner scanner = new Scanner(System.in);
System.out.println(" ");
//Files can end in things other than .txt, so if you make it say that you need
// to do validation that data.endsWith(".txt") == true
System.out.println("Please enter the name of a file with .txt at the end.");
System.out.println(" ");
//Do you use data anywhere else? Or is this just an arbitrary string holder
data = scanner.nextLine();
File inputFile = new File(data);
Scanner reader = new Scanner(inputFile);
while(reader.hasNext()){
store.add(reader.nextDouble());
}
calculateSum(store);
store.clear();
}
//Call it arrayList or something that isn't ArrayList I'm stunned that compiles.
private static void calculateSum(ArrayList<Double> ArrayList){
//if you are declaring it a double sum = 0.0 <--- don't be lazy
double sum = 0;
double avg = 0;
double total = 0;
//Camel case me: totalRainfall or totalRainFall
double totalrainfall = 0;
//Are you still on java 1.4? If not use a foreach loop, while less efficient at higher
// data sets it will not be noticeable with the smaller data set
Iterator<Double> iterator = ArrayList.iterator();
while(iterator.hasNext()){
sum += iterator.next();
}
total = ArrayList.size();
//Need to do a conditional check so you dont divide by 0
avg = (sum / total);
System.out.println(" ");
System.out.println("The total rainfall is " + totalrainfall);
}
答案 5 :(得分:0)
package multiples;
import java.util.Scanner;
public class MulTiples{
public static void main(String []args)
{
int i =0;
while(i<100)
{
if ( i%3==0)
{
System.out.println("hutt");
}
if (i%7==0)
{
System.out.println("hike");
}
System.out.println(i);
i = i+1;
}
}
}