我需要使用.txt
文件从文本文件中执行以下代码。我在使用文件中的扫描仪时遇到问题。 b.txt
中包含以下文字输入:
40.3 35.8 29.6 45 17.8 19.2 38.6 31.5 27.8 39.9 ......
有人可以帮我解决这个问题吗?
import java.util.*;
public class arrayLabFirst
{
public static void main(String[] args)
{
double[ ] temperatures = new double[10];
Scanner inFile = new Scanner(new File(“b.txt”));
temeratures[]=infile.hasNext();
for (int index = 0; index < temperatures.length; index++)
temperatures[index] = input.nextDouble();
printArray(temperatures);
System.out.println("The average of the temperatures entered is: " + arrayAverage(temperatures));
System.out.println("The greatest temperature is: " + greatest(temperatures));
System.out.println("The lowest temperature is: " + lowest(temperatures));
}
public static void printArray(double[] temperatures)
{
for (int i = 0; i < temperatures.length; i++)
System.out.println(temperatures[i]);
}
public static double arrayAverage(double[] temperatures)
{
double sum = 0;
for (int i = 0; i < temperatures.length; i++)
sum += temperatures[i];
return sum/temperatures.length;
}
public static void countDays(double[] temperatures)
{
int aboveCount = 0;
int belowCount = 0;
for (int i = 0; i < temperatures.length; i++)
{
if (i > 32)
aboveCount++;
else
belowCount++;
}
System.out.println("number of days above freezing: " + aboveCount);
System.out.println("number of days below freezing: " + belowCount);
}
public static double greatest(double[] temperatures)
{
int maxIndex = 0;
for (int index = 1; index < temperatures.length; index++)
if (temperatures[maxIndex] < temperatures[index])
maxIndex = index;
return temperatures[maxIndex];
}
public static double lowest(double[] temperatures)
{
int minIndex = 0;
for (int index = 1; index < temperatures.length; index++)
if (temperatures[minIndex] > temperatures[index])
minIndex = index;
return temperatures[minIndex];
}
}
答案 0 :(得分:1)
我并不是真的理解所有的消极性,但让我快速解释他们所谈论的内容。您正在 inFile 中使用 hasNext()方法,该方法返回一个布尔值,以确定是否具有“下一个”值。在这种情况下,它会检查是否有下一个双倍。问题是你要为这个布尔值分配一个双精度数组,你不能这样做。
以下是我使用您的代码工作的内容;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class arrayLabFirst
{
public static void main(String[] args) throws FileNotFoundException
{
double[ ] temperatures = new double[10];
Scanner inFile = new Scanner(new File("data.txt"));
for (int index = 0; index < temperatures.length; index++)
if(inFile.hasNext())
temperatures[index] = inFile.nextDouble();
else
continue;
printArray(temperatures);
System.out.println("The average of the temperatures entered is: " + arrayAverage(temperatures));
System.out.println("The greatest temperature is: " + greatest(temperatures));
System.out.println("The lowest temperature is: " + lowest(temperatures));
inFile.close();
}
public static void printArray(double[] temperatures)
{
System.out.print("Input Array:\n[");
for (int i = 0; i < temperatures.length; i++)
{
System.out.print(temperatures[i]);
if(i < temperatures.length - 1)
{
System.out.print(", ");
}
}
System.out.println("]\n");
}
public static double arrayAverage(double[] temperatures)
{
double sum = 0;
for (int i = 0; i < temperatures.length; i++)
sum += temperatures[i];
return sum/temperatures.length;
}
public static void countDays(double[] temperatures)
{
int aboveCount = 0;
int belowCount = 0;
for (int i = 0; i < temperatures.length; i++)
{
if (i > 32)
aboveCount++;
else
belowCount++;
}
System.out.println("number of days above freezing: " + aboveCount);
System.out.println("number of days below freezing: " + belowCount);
}
public static double greatest(double[] temperatures)
{
int maxIndex = 0;
for (int index = 1; index < temperatures.length; index++)
if (temperatures[maxIndex] < temperatures[index])
maxIndex = index;
return temperatures[maxIndex];
}
public static double lowest(double[] temperatures)
{
int minIndex = 0;
for (int index = 1; index < temperatures.length; index++)
if (temperatures[minIndex] > temperatures[index])
minIndex = index;
return temperatures[minIndex];
}
}