简体   繁体   English

Java do/while 循环问题? 如何计算循环内的字符串

[英]Java do/while loop problem? How to count the string inside the loop

Here is the problem I was given: Write a program that takes website names as keyboard input until the user types the word 'stop' also count how many of the website names are commercial website names (ie, end with .com), and output that count.这是我遇到的问题:编写一个程序,将网站名称作为键盘输入,直到用户输入“停止”这个词,同时计算有多少网站名称是商业网站名称(即以 .com 结尾),然后输出那个数。

I can't figure out how to count the commercial website inside the loop and how to keep adding it if the user keeps adding more.我不知道如何计算循环内的商业网站,以及如果用户不断添加更多,如何继续添加它。

This is my code这是我的代码

public class WebsiteNames {
    public static void main(String[] args) {

        //variables
        int commercialWebsite = 0;
        String commercialNames = "com";
        String website;
        final String SENTINEL = "stop";

        //scanner
        Scanner scan = new Scanner(System.in);
        System.out.print("Please enter a website url ");
        website = scan.next();

        //lenght of the website
        String substring = website.substring(website.length() - 3);

        do {
            System.out.print("Please enter a website url ");
            website = scan.next();
            substring = website.substring(website.length() - 3);

        } while (!SENTINEL.equals(website));
        System.out.println("There were " + commercialNames + " commercial websites entered");

    }
}
import java.util.Scanner;

public class WebsiteNames {

    public static void main(String[] args) {
        // variables
        int commercialWebsite = 0;
        String commercialNames = ".com";
        String website;
        final String SENTINEL = "stop";

        // scanner
        Scanner scan = new Scanner(System.in);

        do {
            System.out.print("Please enter a website url (or 'stop' to terminate): ");
            website = scan.nextLine();
            if (website.endsWith(commercialNames)) {
                commercialWebsite++;
            }
        } while (!SENTINEL.equals(website));

        System.out.println("There were " + commercialWebsite + " commercial websites entered");
    }
}
  • Use method nextLine instead of method next() .使用方法nextLine而不是方法next()
  • Use method endsWith rather than method substring() since you are looking for Web sites whose names end with .com .使用方法endsWith而不是方法substring()因为您正在寻找名称以.com结尾的网站。
  • You are using a do-while loop so no need to ask the user for input before entering the loop.您正在使用do-while循环,因此在进入循环之前无需询问用户输入。
  • Increment the counter, ie commercialWebsite if the entered Web site ends with the desired suffix.如果输入的网站以所需的后缀结尾,则增加计数器,即commercialWebsite网站。

i have done this with for loop.Its working as your requirements.我已经用 for 循环完成了这个。它按照你的要求工作。

public static void main (String[] args) {
  Scanner scan = new Scanner(System.in);
    int count=0;
    System.out.println("Enter website names:");
    String websites = scan.nextLine();
    String com = "com";
    String[] website = websites.split(" ");
   for (int i=0;i<website.length ;i++ ) 
    {
        if(website[i].substring(website[i].length()-3).equals(com)){
            count++;
        }  
        System.out.println("Names "+website[i]);
    }
    System.out.println("website Names="+websites);
    System.out.println("count "+count);
}

A simple way to achieve this is to use string.contains()实现此目的的一种简单方法是使用string.contains()

Example for your application with some changes by my side:您的应用程序示例,我有一些更改:

 String website;
    while(true)
    {
        System.out.print("Please enter a website url ");
        website = scan.nextLine();
        if(website.equalsIgnoreCase("stop"))
            break; // Go out of the loop

        if(website.contains(".com"))// if it contains the .com we increment the counter
            commercialWebsites++;

    }

    System.out.println("There were " + commercialWebsites + " commercial websites entered" );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM