[英]Why does the compiler complain “while expected” when I try to add more code?
Write a program with a word containing @ character as an input. 编写一个包含@字符的单词作为输入的程序。 If the word doesn't contain @, it should prompt the user for a word with @. 如果单词不包含@,则应使用@提示用户输入单词。 Once a word with @ is read, it should output the word then terminate. 读取带有@的单词后,应输出该单词然后终止。
This is what I have done so far: 到目前为止,这是我所做的:
public class find {
public static void main(String[] args) {
System.out.println(" Please enter a word with @ ");
Scanner scan = new Scanner(System.in);
String bad = "@";
String word = scan.next();
do
if (!word.contains(bad))
System.out.println(" Please try again ");
else
System.out.println(" " + word);
while (!word.contains(bad));
}
}
I can get it to terminate after a word containing "@" is given as input, but if I try to add a Scanner to the line after "please try again", it says while expected
. 输入一个包含“ @”的单词后,我可以使它终止,但是,如果在“请重试”之后尝试在行中添加扫描仪,它会while expected
。
I think issue is you are missing surrounding braces for do/while: 我认为问题是您在做/做时缺少周围的括号:
do
if (!word.contains( bad ))
System.out.println( " Please try again " );
else
System.out.println( " " + word);
while ( !word.contains( bad ));
should be: 应该:
do
{
if (!word.contains( bad ))
System.out.println( " Please try again " );
else
System.out.println( " " + word);
}while ( !word.contains( bad ));
Some people may not like this, but my suggestion is always use open/close braces. 某些人可能不喜欢这样,但我的建议是始终使用开/大括号。 In this case, for the code if/else also. 在这种情况下,代码也为if / else。 It avoids lot of confusion. 它避免了很多混乱。
This is where your problem lies: 这是您的问题所在:
do
if (!word.contains(bad))
System.out.println(" Please try again ");
else
System.out.println(" " + word);
while (!word.contains(bad));
You need to put braces from where the loop starts until it ends. 您需要在循环开始的地方放括号直到结束。 |So this thing should like: |所以这个东西应该像:
do {
if (!word.contains(bad))
System.out.println(" Please try again ");
else
System.out.println(" " + word);
} while(!word.contains(bad));
For Better Practice You should Check do...while
loops here . 为了更好的实践,您应该do...while
此处检查do...while
循环。
There are two issues. 有两个问题。
Also I prefer while
loop better in the case as opposed to do-while
loop as below. 另外do-while
与下面的do-while
循环相比,我更喜欢while
循环。
Scanner scan = new Scanner ( System.in );
String required= "@";
System.out.println( " Please enter a word with @ " );
String word = scan.next() ;
//check if the right word(containing @) is entered,
//if not then loop until it is enteres
while((!word.contains(required)){
System.out.println( " Please try again " );
//read the new word as input from the user
word = scan.next() ;
}
//right word is entered, display it
System.out.println(word);
Also please note that when you use scan.next()
, it reads each word separately if entered in the same line. 另外请注意,当您使用scan.next()
,如果在同一行中输入,它将分别读取每个单词。
The problem with your code is it is not re-reading the word in your loop. 您的代码的问题在于它没有重新读取循环中的单词。 Modify your loop like this (minimum change to your code). 像这样修改循环(最少更改代码)。
do {
word = scan.next();
if (!word.contains(bad))
System.out.println(" Please try again ");
else
System.out.println(" " + word);
}
while (!word.contains(bad));
And yes as others have pointed out try to use braces especially with nested constructs. 是的,正如其他人指出的那样,请尝试使用大括号,尤其是在嵌套结构中使用大括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.