2011-04-10 143 views
0

我的第二個if(輸出)循環,不會停止循環。 如果對故障排除有任何建議?如果循環不會停止循環

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; 
    } 
} 
+2

waaaaaaay代碼太多。你的問題並不清楚。 – 2011-04-10 21:41:00

+0

方式太多代碼 – 2011-04-10 21:41:10

+0

你是不是指'while(inFile.hasNext())'循環? – 2011-04-10 21:42:35

回答

0

那是因爲你的ouptutFileName並沒有改變其不若循環是while循環會無限..我,你應該從控制檯再次以輸入while循環結束之前猜測。所以只是在while循環結束時,你應該做的

String inputFile = inputFileName(); 
String outputFile = outputFileName(); 
+0

感謝你瞭解它。 ((!)inputFile.equals(「exit」)||(!outputFile.equals(「exit」)))&&!sentinel),因爲這實際上導致它無限循環 – Max 2011-04-10 21:47:40

1

這是一噸的代碼,但我認爲這個問題可能只是在你的while循環的條件。您有:

while (!inputFile.equals("exit") || (!outputFile.equals("exit")))

所以,如果你已經把在出口處爲輸入文件和輸出文件的循環將退出。但是,只有在第一次使用這兩個文件時,您纔會檢查一次輸入。如果你想讓循環每次取兩個新的文件名,那麼你需要在while循環的結尾添加,就像harshit建議的那樣。如果在while循環的末尾添加String inputFile = inputFileName();String outputFile = outputFileName();,則每次出現循環時都會輸入新的文件名,並且您應該能夠退出它。