所以我能够找出我的问题并且成功了,但我仍然希望任何人都可以输入是否以及如何通过使用三元运算符来简化此语法。
import java.util.Scanner;
import java.io.*;
public class Midterm
{
public static void main(String[]args) throws Exception
{
final String FILE = "integers.txt";
final String RELATIVE_DIRECTORY_FILE = "integers.txt ";
int positiveNumberCount = 0,
negativeNumberCount = 0;
String dataItem;
double sum = 0;
double sumForAverage = 0;
double average = 0;
File inputFile = new File(RELATIVE_DIRECTORY_FILE);
if(!inputFile.exists())
{
System.out.printf("%n%s%s%s", "The file, ", RELATIVE_DIRECTORY_FILE, " does not exist");
}
if(inputFile.exists())
{
System.out.printf("%s%s%s", "The file ", RELATIVE_DIRECTORY_FILE, "was found");
Scanner file = new Scanner(inputFile);
//***if i need to use a delimiter *****
//file.useDelimiter(",") where in between " " is my type of delimiter
while(file.hasNext())
{
dataItem = file.nextLine();
double numericalDataItem = Double.parseDouble(dataItem);
boolean isPositive = false;
boolean isNegative = false;
if(numericalDataItem > 0)
{
isPositive = true;
positiveNumberCount++;
}
else if(numericalDataItem < 0)
{
isNegative = true;
negativeNumberCount++;
}
sum += Double.parseDouble(dataItem);
sumForAverage = positiveNumberCount + negativeNumberCount;
average = sum/sumForAverage;
}
System.out.printf("%n%s%d%n%s%d%n%s%.2f%n%s%.2f", "Positive Number Count: ", positiveNumberCount,
"Negative Number Count: ", negativeNumberCount, "Sum Of Numbers: ", sum, "Average of Numbers: ", average);
}
}
}