java - 所以这就是代码:但它从不考虑我写的第一个字符串?即使它以第一个单词结尾也不算数
问题描述
但它从不考虑我写的第一个字符串?即使它仅以第一个单词结尾,它也不算数。
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");
解决方案
您需要将第二个移动word = scan.nextLine();
到循环的末尾。
此外,您可能不会一直在循环中打印提示。
while (!word.equals("exit")) {
count++;
if (word.endsWith("a")) {
end++;
}
word=scan.nextLine();
}
推荐阅读
- r - 错误:“rjags”的包或命名空间加载失败
- php - 如何从php获取数据到html
- mongodb - 查询 MongoDB 中的嵌套对象数组
- javascript - 与关键和子组件反应的关键道具警告
- json - 如何在 App Inventor 中解析 JSON 文件?
- python - 在字典 Python 中访问列表中的值
- swift - Swift Combine 从 func 返回 Publisher
- database - 有两个或多个具有相同属性的关系的主要原因是什么?
- java - 如何优化有关 if 条件的逻辑?
- javascript - SVG - 使用带有转换元素的 checkIntersection()