提示用户输入一系列字符并输出元音的数量 [英] prompts the user to input a sequence of characters and output the number of vowels

查看:100
本文介绍了提示用户输入一系列字符并输出元音的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import java.util。*;

import java.lang.Character。*;


public class IsVowel

{


静态字母元数[] = {'''',''e'',''我',''o'',''你', ''y''};


public static boolean isVowel(char s)

{


boolean pass = false;

for(int i = 0; i< 6; i ++)

if(s == vowels [i])pass = true;

返回通行证;

System.out.print(输入元音数量:);

System.out.println();

}

}

import java.util.*;
import java.lang.Character.*;

public class IsVowel
{

static char vowels[] = { ''a'', ''e'', ''i'', ''o'', ''u'', ''y'' };

public static boolean isVowel(char s)
{

boolean pass = false;
for(int i=0; i<6; i++)
if (s == vowels[i]) pass = true;
return pass;
System.out.print("Enter the number of vowels":);
System.out.println();
}
}

推荐答案


import java.util。 *;

import java.lang.Character。*;


公共类IsVowel

{

static char vowels [] = {'''',''e'',''我',''o'','''',''y''};


public static bool ean isVowel(char s)

{


boolean pass = false;

for(int i = 0; I&10 6; i ++)

if(s == vowels [i])pass = true;

return pass;

System.out.print(" ;输入元音数量:);

System.out.println();

}

}
import java.util.*;
import java.lang.Character.*;

public class IsVowel
{

static char vowels[] = { ''a'', ''e'', ''i'', ''o'', ''u'', ''y'' };

public static boolean isVowel(char s)
{

boolean pass = false;
for(int i=0; i<6; i++)
if (s == vowels[i]) pass = true;
return pass;
System.out.print("Enter the number of vowels":);
System.out.println();
}
}



1.)每次发布代码时请使用代码标签

2.)您不必在java.lang包中导入类。

3.)编写提示输入的主方法并使用Scanner类获取它,然后在该main方法中调用isVowel方法。如果你遇到麻烦,你可以再次发帖。

1.) Please use code tags everytime when posting code
2.) You do not have to import classes in the java.lang package.
3.) Write the main method which prompts for the input and take it using the Scanner class then call your isVowel method in that main method. You can post again if you get stuck with that.


我用你的方法看到的问题是你混淆了int返回的布尔返回。你的问题说该方法应该输出元音的数量。这是你应该做的。声明一个名为count的int类型或与元音数组具有相同范围的类似的东西,用0初始化它。将方法的名称更改为vowelNumber或类似的东西。删除布尔变量,传递。你没有检查布尔人。您的决策声明应该是:如果在字符,元音数组中找到char变量s,那么让它反映在计数中。尽量不要粗心,y不是元音。最后一点,在确保您的返回是代码中的最后一个语句之后。那就是惯例。跟着它。

我决定写一个反映上述更正的代码

The problem I see with your method is you confused a Boolean return for an int return. Your question said the method was supposed to output the number of vowels. This is what you should do. Declare an int type named count or something similar with same scope as the vowels array, initialize it with 0. change the name of the method to vowelNumber or something similar. Remove the Boolean variable, pass. You?re not checking for Booleans. Your decision statement should be: if the char variable s is found in the array of chars, vowels, then let it reflect in count. Try not to be careless, y is not a vowel. On a final note, after everything make sure your return is the last statement in your code. That?s the convention. Follow it.
I decided to write a code that reflects the above corrections

展开 | 选择 | Wrap | 行号


这里有一个相当神秘的方式,可以看到正则表达式的强大功能:
And here''s a rather cryptic way that shows a glimpse of the power of regular expressions:
展开 | 选择 | Wrap | 行号


这篇关于提示用户输入一系列字符并输出元音的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆