【问题标题】:WAP to input a sentence and print how many times "is" appears in that sentence [Java]WAP 输入一个句子并打印该句子中出现了多少次“is” [Java]
【发布时间】:2020-05-09 05:47:34
【问题描述】:

WAP 输入一个句子并打印“is”在该句子中出现的次数 [Java] 只能使用字符串函数 只能使用扫描器类

没有数组函数或拆分句子

【问题讨论】:

  • 请分享您的尝试,然后询问您是否卡在某个地方。没有人来解决你的作业。
  • 我被要求完成 50 个程序,但我被困在了这里,所以我在这里问,我不知道从哪里开始。
  • 这有帮助吗? Count the number of Occurrences of a Word in a String对了,什么是WAP?

标签: java string


【解决方案1】:
import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String input;
        input = scan.nextLine();
        int index = input.indexOf("is");
        int count = 0;
        while (index != -1) {
            count++;
            input = input.substring(index + 1);
            index = input.indexOf("is"); 
        }
        System.out.println("No of *is* in the input is : " + count);
    } 
}

【讨论】:

  • 以下句子中is这个词出现了多少次? 这个列表很长。现在你的代码说它出现了多少次?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-30
  • 1970-01-01
  • 2014-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多