我的第二个 if(output) 循环不会停止循环。 在解决循环问题时是否有任何建议?
import java.io.*;
import java.util.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author maxknee
*/
public class MonthTemps {
public static void main(String[] args) throws FileNotFoundException
{
//declare variables
Boolean inputTest = false;
String inputFile = inputFileName();
String outputFile = outputFileName();
Boolean sentinel = false;
double highTemp, lowTemp, averageTemp, range;
double maxTemp = Double.MAX_VALUE;
double minTemp = Double.MIN_VALUE;
double highSum, lowSum, yearlySum, rangeSum;
double totalAvgHigh = 0;
double totalAvgLow = 0;
double totalAvgYearly = 0;
double totalRangeAvg = 0;
String month;
int i = 0;
//Exit do-while loop
while (!inputFile.equals("exit") || (!outputFile.equals("exit")))
{
inputTest = inputValidName(inputFile);
//Open files for reading
Scanner inFile = new Scanner(new File(inputFile));
//Open file for writing to.
PrintWriter outFile = new PrintWriter("averageTemperatures.txt");
//Input validation for input file name
if (inputTest)
{
Boolean outputTest = outputValidName(outputFile);
//input validation for output file name
if (outputTest)
{
//Creating headers for console output and text output file
System.out.println("---------------------------------------------------------------------------------");
System.out.println(" Month | High Temperature | Low Temperature | Average Temperature | Range ");
System.out.println("---------------------------------------------------------------------------------");
outFile.println("---------------------------------------------------------------------------------");
outFile.println(" Month | High Temperature | Low Temperature | Average Temperature | Range ");
outFile.println("---------------------------------------------------------------------------------");
//do while loop to read file
while (inFile.hasNext())
{
//extract Month name from file
month = inFile.next();
//Remove comma for better presentation
String formatMonth = month.replace(',', ' ');
//calculate high temperature from text file
highTemp = inFile.nextDouble();
//extract low temperature file from text file
lowTemp = inFile.nextDouble();
//Calculate averaget Temperature from values
averageTemp = (highTemp + lowTemp)/2;
//Calculate Range
range = (highTemp - lowTemp);
//Calculate low temperature
minTemp = Math.min(minTemp, lowTemp);
//Calculate high temperature
maxTemp = Math.max(maxTemp, highTemp);
//Sum up high temperatures
highSum =+ highTemp;
//Sum up low Temperatures
lowSum =+ lowTemp;
//calculate yearly sum
yearlySum =+ averageTemp;
//Calculate average of all the high temperatures
totalAvgHigh = highSum/12;
//Calculate average of all low temperatures
totalAvgLow = lowSum/12;
//Calculate the average of the years
totalAvgYearly = yearlySum/12;
//Calculate averge of the range
totalRangeAvg = (highSum +lowSum)/12;
System.out.println(formatMonth + " | " + highTemp + " | " + lowTemp + " | " + averageTemp + " | " + range );
outFile.println(formatMonth + " | " + highTemp + " | " + lowTemp + " | " + averageTemp + " | " + range );
}
//Print out calculated values
System.out.print("Average High Temps: " + totalAvgHigh + " Average Low Temps: " + totalAvgLow + " Total Average: " + totalAvgYearly + " Total Range Average: " + totalRangeAvg);
System.out.println("High Temp " + maxTemp);
System.out.println("Low Temp " + minTemp);
//Formatting
outFile.println("---------------------------------------------------------------------------------");
inFile.close();
outFile.close();
}
//Option to crrect output file name
else
{
String correctOutput = correctOutputFilename();
outputFile = correctOutput;
}
}
//Chance to correct input file name
else
{
String correctInput = correctInputFileName();
inputFile = correctInput;
}
}
}
/**
* output corrected file name if outputfileName is wrong
* @return
*/
public static String correctOutputFilename() {
Scanner console = new Scanner(System.in);
System.out.print("Please enter the correct output file name ");
return console.next();
}
/**
* input corrected file name if inputfileName is wrong
* @return
*/
public static String correctInputFileName() {
Scanner console = new Scanner(System.in);
System.out.print("Please enter the correct input file name ");
return console.next();
}
/**
* collect inputFileName for the input file for program to read
* @return
*/
public static String inputFileName()
{
Scanner console = new Scanner(System.in);
System.out.print("Please enter the filename you wish to open or enter 'exit' to exit ");
String fileName;
fileName = console.next();
return fileName;
}
/**
* collect input for output file
* @return
*/
public static String outputFileName()
{
Scanner console = new Scanner(System.in);
System.out.print("Please enter the file name you wish to output to: ");
return console.next();
}
/**
* test to see if inputfilename is valid
* @param inputFileName
* @return boolean value if inputfileName is correct or not
*/
public static Boolean inputValidName(String inputFileName)
{
Boolean inputName;
if (inputFileName.equals("MonthlyTemperatures.txt"))
{
inputName = true;
}
else {inputName = false;}
return inputName;
}
/**
* to check if outputfilename is correct
* @param outputFileName
* @return return boolean value if outputfilename is valid
*/
public static Boolean outputValidName(String outputFileName)
{
Boolean outputName;
if (outputFileName.equals("averageTemperatures.txt"))
{
outputName = true;
}
else {outputName = false;}
return outputName;
}
}
最佳答案
这是大量代码,但我认为问题可能只是 while 循环中的条件。你有:
while (!inputFile.equals("退出") || (!outputFile.equals("退出")))
因此,如果您为输入文件和输出文件都设置了 exit,则循环将退出。但是,仅在首次使用这两个文件时检查输入一次。如果您希望循环每次都采用两个新文件名,则需要将其添加到 while 循环的末尾,就像 Harrit 建议的那样。如果在 while 循环末尾添加 String inputFile = inputFileName();
和 String outputFile = outputFileName();
,则每次都会输入新的文件名发生循环,您应该能够退出它。
关于java - if循环不会停止循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5614878/