首页 > 解决方案 > 所以这就是代码:但它从不考虑我写的第一个字符串?即使它以第一个单词结尾也不算数

问题描述

但它从不考虑我写的第一个字符串?即使它仅以第一个单词结尾,它也不算数。

Scanner scan=new Scanner(System.in);
System.out.println("type some words and when youre finished type exit. (SENTINEL):");
String word=scan.nextLine();
int count=0;
int end = 0;
while(!word.equals("exit")){
    count++;
    System.out.println("type some words and when youre finished type exit. (SENTINEL):");
    word=scan.nextLine();
    if(word.endsWith("a")){        
        end++;              
    }
}
System.out.println("You typed "+count+" words");
System.out.println(end+" of them ends with the letter  a");

标签: java

解决方案


您需要将第二个移动word = scan.nextLine();到循环的末尾。

此外,您可能不会一直在循环中打印提示。

while (!word.equals("exit")) {
    count++;

    if (word.endsWith("a")) {        
        end++;              
    }
    word=scan.nextLine();
}

推荐阅读