2016-03-26 20 views
-1

Her iki sorun da yorumlarda etiketli getCity yöntemindedir. herhangi bir yardım harika olurdu, eğer okurken gördüğünüz başka bir hata varsa, alabileceğim herhangi bir yardımı kabul edeceğim.Yöntemimde hatalar

//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; 
} 

cevap

0

diziminizi değiştirin. while, do'un gövdesinin dışında olmalıdır.

doğru sözdizimi şöyledir:

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

Yani kod aşağıdaki gibi görünmelidir: sizin için

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)); 

Bir ipucu, if-else deyimini kullanırken, parantez {} için asla unutmam lütfen lütfen.

+0

Yardımlarınız için teşekkürler, bu düzeltildi – James

+0

Çalıştığına sevindim. Bu cevap veya başka bir sorun sorununuzu çözdüyse, lütfen kabul edildi olarak işaretleyin. –

2
do { 
    // code 
} 

geçerli bir ifade değil.

Sen do {} while (booleanExpression);

Sizin ikinci hata aradığınız ilkine kaynaklanmaktadır.


while (checkValidCity(input) == false); 

Sen sonunda noktalı virgül gerekmez, aksi hiçbir şey olmaz. below.Syntax kodunuzu ederken {// bu} yapmak yapmak için

0

Değişim while (koşul)

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)