Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 这就是代码:但它从不考虑我写的第一个字符串?即使它的结尾只有一个字,它也不算_Java - Fatal编程技术网

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();
}

请格式化您的代码并重新表述您的问题,您的问题很难阅读,这个问题已经有了正确的答案,但我想借此机会建议,这是一个完美的情况,可以使用调试器来找出代码无法按预期工作的原因。