Java 这就是代码:但它从不考虑我写的第一个字符串?即使它的结尾只有一个字,它也不算
但它从不考虑我写的第一个字符串?即使它的结尾只有第一个字,它也不算Java 这就是代码:但它从不考虑我写的第一个字符串?即使它的结尾只有一个字,它也不算,java,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
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();
}
请格式化您的代码并重新表述您的问题,您的问题很难阅读,这个问题已经有了正确的答案,但我想借此机会建议,这是一个完美的情况,可以使用调试器来找出代码无法按预期工作的原因。