gpt4 book ai didi

java - Codeabbey 中的元音计数

转载 作者:行者123 更新时间:2023-11-30 03:23:56 24 4
gpt4 key购买 nike

我正在处理 vowel counting problem at coedabbey ,但我的解决方案似乎不起作用。这就是我正在做的事情:

import java.util.Scanner;

public class Solution {
private static Scanner input;

public static void main(final String[] args){

input = new Scanner(System.in);
int amount = input.nextInt();

for(int i = 0 ; i < amount ; i++){
int sum = 0;
String nowa = input.nextLine();

for(int j = 0; j < nowa.length() ; j++){
char x = nowa.charAt(j);

if(x == 'a' || x == 'o' || x == 'u' || x == 'i' || x == 'e' || x == 'y'){
++sum;
}
}
System.out.println(sum+ " ");
}
}
}

但它没有执行正确的行数,并且在我输入输入后始终输出 0 作为行计数。之后,它比我预期的少了一行。

示例运行可能如下所示:

> java Solution
> 3
0
> hello
2
> george
3

但是,我想输入另一行,因为我在开头说了“3”。

最佳答案

nextInt()之后跳过一行,因为它不消耗整行,它只消耗 token

 int amount = input.nextInt();
input.nextLine();

Demo

关于java - Codeabbey 中的元音计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30716272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com