这两个问题都在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 :(得分:2)
do {
// code
}
不是有效的表达。
您正在寻找do {} while (booleanExpression);
您的第二个错误是由于第一个错误。
while (checkValidCity(input) == false);
最后你不需要分号,否则什么都不会发生。
答案 1 :(得分:0)
将您的代码更改为以下。执行for while的时间为{// this} while(condition)
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)
答案 2 :(得分: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
声明时,请不要忘记括号{}
。