简体   繁体   English

当我尝试添加更多代码时,为什么编译器会抱怨“虽然期望”?

[英]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. 有两个问题。

  1. Your code is not using the braces properly 您的代码未正确使用花括号
  2. you are not attempting to read the new word if right word is not entered. 如果未输入正确的单词,则不会尝试读取新单词。

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.

相关问题 为什么当我尝试覆盖静态方法时编译器没有抱怨? - Why doesn't the compiler complain when I try to override a static method? Java:为什么编译器会在这里抱怨? - Java: Why does the compiler complain here? 当我将SocketException扔回IOException时,为什么Java会抱怨? - Why does java complain when I rethrow SocketException to be caught by IOException? 当我用两个分号结束一行时,编译器不会抱怨。为什么? - Compiler doesn't complain when I ended a line with two semicolons. Why? 为什么这个Java代码有效?编译器不抱怨关闭 - Why is this Java code working? compiler doesn't complain about closure 为什么编译器抱怨非法启动表达式错误消息? - Why does the compiler complain with illegal start of expression error message? 整数文字作为字节变量/参数:编译器何时确切发出抱怨? - integer literals as byte variables/parameters: when exactly does the compiler complain? 编译器没有抱怨我捕获了一个永远不会抛出的异常 - Compiler does not complain that I catch an exception that can never be thrown 为什么编译器不抱怨这个错误? - Why doesn't the compiler complain about this error? 当我尝试无休止地更新JTextField时,为什么程序会冻结? - Why does my program freeze when I try to update a JTextField with an endless while?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM