2016-03-26 26 views
-1

這兩個問題都在getCity方法中,在註釋中標註。 任何幫助都很棒,如果您在閱讀時發現任何其他錯誤,我會接受任何幫助。我的方法中的錯誤

//DO NOT ALTER THE MAIN METHOD 
public static void main(String[] args) { 
    //determine input file 
    String fileName = "coven_consulting.txt"; 
    //print method to output breakdown 
    printReport(fileName); 
} 

/* printReport - take the file name, open the file, read and process data, print out report 
* input: String fileName - the name of the file containing the data 
* returns: nothing 
*/ 
private static void printReport(String fileName) { 
    //implement this method 
} 

/* getCity - ask the user for a city, loop unitl the user gives you a valid one 
* input: none 
* returns: String - the name of the validated city 
*/ 
@SuppressWarnings("empty-statement") 
private static String getCity() { 
//implement this method, change the return statement to suit your needs 
    Scanner keyboard = new Scanner (System.in); 
    String input; 
    String city = ""; 

    do { 
     System.out.print("Which city do you want a report for?"); 
     input = keyboard.next(); 

     if (checkValidCity(input) == true) 
     input = city; 
     while (checkValidCity(input) == false); 
      System.out.print("Not a city we consult in, try another...");  
    } //Error: says while expected 
    return city; //Error: says illegal start to expression 
} 

private static boolean checkValidCity(String input) { 
    //implement this method, change the return statement to suit your needs 
    boolean result; 
    if (input.equalsIgnoreCase ("Uberwald") || 
     (input.equalsIgnoreCase ("Pseudopolis")) || 
     (input.equalsIgnoreCase ("Quirm")) || 
     (input.equalsIgnoreCase ("AnkhMorpork"))) 
     result = true; 
    else 
     result = false; 
    return result; 
} 

回答

0

改變你的語法。 while應該在do之外。

正確的語法是:

do { 
    // code, bla, blaaa 
} while (condition); 

所以,你的代碼應該是這樣的:

do { 
    System.out.print("Which city do you want a report for?"); 
    input = keyboard.next(); 
    if (checkValidCity(input)) { 
     input = city; 
    } 
    System.out.print("Not a city we consult in, try another...");  
} while (!checkValidCity(input)); 

一個提示,請在使用if-else聲明,請過目不忘的括號{}

+0

感謝您的幫助,這固定它 – James

+0

很高興它的工作。如果此答案或任何其他人解決了您的問題,請將其標記爲已接受。 –

2
do { 
    // code 
} 

不是有效的表達。

您正在尋找do {} while (booleanExpression);

你的第二個錯誤是由於第一個。


while (checkValidCity(input) == false); 

你不需要在最後一個分號,否則什麼也不會發生。

0

更改您的代碼以below.Syntax爲做而做是{//這}而(條件)

do { 
      System.out.print("Which city do you want a report for?"); 
      input = keyboard.next(); 



      if (checkValidCity(input) == true) 
      input = city; 


     }  while (checkValidCity(input) == false)