当要求用户输入时如何打破 while 循环?

发布于 2025-01-11 20:28:03 字数 1036 浏览 0 评论 0原文

当用户输入空行而不是国家/地区时,我需要尝试打破 while 循环。这是我到目前为止所做的代码,它似乎并不想结束询问国家/地区的循环:

public void userInterface()
    {
    // interactive interface
      Scanner input = new Scanner(System.in);

      System.out.println("Enter the date:");
      String date = input.nextLine();

      ArrayList<String> countries = new ArrayList<String> ();
      
      System.out.println ("Enter the list of countries (end with an empty line):");
      
      while (input.hasNext()) {
         String country = input.nextLine();
         
         if (country.isEmpty()){
            break;
         } 
             
         char c = country.charAt(0);
         
         if (Character.isUpperCase(c)==false){
            System.out.println ("Type with Capital Letter!");
         } else {
            countries.add(country);         
         } 
      }
    
   
   }

用户输入应如下所示:

Enter the date:
2022-02-17
Enter the list of countries (end with an empty line):
Norway
Suriname
South Namibia

i need to try to break the while loop when a user enters an empty line instead of countries. Here is the code I have done so far and it does not seem to want to end the loop of asking for countries:

public void userInterface()
    {
    // interactive interface
      Scanner input = new Scanner(System.in);

      System.out.println("Enter the date:");
      String date = input.nextLine();

      ArrayList<String> countries = new ArrayList<String> ();
      
      System.out.println ("Enter the list of countries (end with an empty line):");
      
      while (input.hasNext()) {
         String country = input.nextLine();
         
         if (country.isEmpty()){
            break;
         } 
             
         char c = country.charAt(0);
         
         if (Character.isUpperCase(c)==false){
            System.out.println ("Type with Capital Letter!");
         } else {
            countries.add(country);         
         } 
      }
    
   
   }

the user input should look as follows:

Enter the date:
2022-02-17
Enter the list of countries (end with an empty line):
Norway
Suriname
South Namibia

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷默言语 2025-01-18 20:28:03

您检查 hasNext() 但应该检查 hasNextLine()

但是,请不要执行以下任一操作:

while (true) {
     String country = input.nextLine();
     
     if (country.isEmpty()){
        break;
     } 
         
     char c = country.charAt(0);
     
     if (!Character.isUpperCase(c)){
        System.out.println ("Type with Capital Letter!");
     } else {
        countries.add(country);         
     } 
  }

Your checking for hasNext() but should be checking for hasNextLine().

However, don’t do either:

while (true) {
     String country = input.nextLine();
     
     if (country.isEmpty()){
        break;
     } 
         
     char c = country.charAt(0);
     
     if (!Character.isUpperCase(c)){
        System.out.println ("Type with Capital Letter!");
     } else {
        countries.add(country);         
     } 
  }
冬天旳寂寞 2025-01-18 20:28:03

您还可以在 do while 循环之前设置一个空的国家/地区字符串,并检查国家/地区最终是否不为空:

public void userInterface()
{
    // interactive interface
    Scanner input = new Scanner(System.in);

    System.out.println("Enter the date:");
    String date = input.nextLine();

    ArrayList<String> countries = new ArrayList<String> ();

    System.out.println ("Enter the list of countries (end with an empty line):");

    String country = "";
    do
    {
        country = input.nextLine();
        if(!country.isEmpty()) {
            char c = country.charAt(0);

            if (Character.isUpperCase(c) == false)
            {
                System.out.println("Type with Capital Letter!");
            }
            else
            {
                countries.add(country);
            }
        }
    } while(!country.isEmpty());
}

You could also set an empty country string before a do while loop and check for the the country not being empty in the end:

public void userInterface()
{
    // interactive interface
    Scanner input = new Scanner(System.in);

    System.out.println("Enter the date:");
    String date = input.nextLine();

    ArrayList<String> countries = new ArrayList<String> ();

    System.out.println ("Enter the list of countries (end with an empty line):");

    String country = "";
    do
    {
        country = input.nextLine();
        if(!country.isEmpty()) {
            char c = country.charAt(0);

            if (Character.isUpperCase(c) == false)
            {
                System.out.println("Type with Capital Letter!");
            }
            else
            {
                countries.add(country);
            }
        }
    } while(!country.isEmpty());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文