我觉得我完成了大部分程序。我不相信我真正了解循环是如何工作的。我已经检查过试图修复这个程序,但此时我不知道从哪里开始。
更新*我想我并不清楚我在做什么。试图为他们单独创建方法。数字的方法,元音的方法等我在这方面相当新,感谢你们迄今为止给予的帮助
{
static Scanner kb = new Scanner(System.in);
public static void main (String[] args)
{
String s;
int x;
System.out.println("Enter a String(EOF to end)");
s=kb.nextLine();
System.out.println(("You have entered ") + s.length() + ("characters and it contains the following"));
System.out.println(whitespace + ("whitespace characters."));
System.out.println(digits + ("digits."));
System.out.println(letters + ("letters."));
System.out.println(vowels + ("vowels."));
}
public static int whitespace(String s){
int x;
int whitespace = 0;
if (Character.isWhitespace(s.charAt(x))){
for containsWhitespace=true{
whitespace++;}}
return whitespace;}
public static int digits(String s){
int digits = 0;
int x;
if (Character.isDigit(s.charAt(x))){
return digits ++;}}
public static int letters(String s){
int letters = 0;
int x;
if (Character.isLetter(s.charAt(x))){
letters ++;}
return letters;}
public static int vowels(String s){
int vowels = 0;
int x;
char c = s.charAt(x);
if((c=='a')||(c=='e')||(c=='i')||(c=='o')||(c=='u')||(c=='A')||(c=='E')||(c=='I')||(c=='O')||(c=='U')){
vowels ++;
}
return vowels;}
public static int length(String s){
for (x=0; x<s.length(); x++)
{
x = s.charAt(x);}
}
}
答案 0 :(得分:1)
这不是有效的for
循环:
for containsWhitespace=true{
whitespace++;}
}
有两种方法没有返回任何内容,例如:
public static int digits(String s) {
public static int whitespace(String s){
打印不会找到变量,因为它们未被声明。
我建议您下载Eclipse,NetBeans或其他一些IDE来解析代码并提供语法突出显示,找到错误和警告会更容易,并会指出它们。
答案 1 :(得分:0)
您需要的是,您需要定义4个静态类级变量,如:
static int whitespace, digits, letters, vowels;
然后在你的所有方法中,将它变为void并期望一个字符而不是字符串。所以你的数字方法就像:
public static void digits(char s) {
if (Character.isDigit(s)) { digits ++;}
}
使用适当的计数器变量递增将相同的逻辑应用于所有方法。
最后在main方法中,您需要迭代字符串中的每个字符,如下所示:
for (char ch : s) {
digits(ch);
//call other methods as well
}
System.out.println("You have entered " + s.length() + "characters and it contains the following");
System.out.println(whitespace + "whitespace characters.");
System.out.println(digits + "digits.");
System.out.println(letters + "letters.");
System.out.println(vowels + "vowels.");
答案 2 :(得分:0)
我之前提出了类似的问题,我发布了这段代码:
public static void main(String... args)
{
int answer = 0;
Scanner input = null;
do
{
input = new Scanner(System.in);
System.out.print("Type a sentence and this program will tell you\nhow many vowels there are (excluding 'y'):");
String sentence = input.nextLine();
int vowels = 0;
String temp = sentence.toUpperCase();
for (int i = 0; i < sentence.length(); i++)
{
switch((char)temp.charAt(i))
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowels++;
}
}
System.out.println("The sentence: \"" + sentence + "\" has " + vowels + " vowels");
System.out.print("Would you like to check another phrase in the Vowel Counter? if so Press 1 if not press any other key... ");
String tempNum = input.next();
try
{
answer = Integer.parseInt(tempNum);
} catch (NumberFormatException e)
{
answer = 0;
}
System.out.println();
} while (answer == 1);
input.close();
System.out.println("Have a nice day");
}
要计算空格,数字,辅音等,您需要做的就是使用Character类来确定它是数字,空格还是字母,以及用于区分元音和辅音的先前代码:
public static void main(String... args)
{
int answer = 0;
Scanner input = null;
do
{
input = new Scanner(System.in);
System.out
.print("Type a sentence and this program will tell you\nhow many vowels there are (excluding 'y'):");
String sentence = input.nextLine();
int vowels = 0;
int consonants = 0;
int digits = 0;
int whitespaces = 0;
String temp = sentence.toUpperCase();
for (int i = 0; i < sentence.length(); i++)
{
char character = (char) temp.charAt(i);
if(Character.isDigit(character))
{
digits++;
}
else if(Character.isWhitespace(character))
{
whitespaces++;
}
else if(Character.isAlphabetic(character))
{
switch (character)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
vowels++;
break;
default:
consonants++;
break;
}
}
else
{
System.out
.println("ERROR: Not an alphanumeric character or a whitespace: '"
+ character + "'");
}
}
System.out.println("The string: \"" + sentence + "\" has "
+ vowels + ((vowels != 1) ? " vowels, " : " vowel, ")
+ digits + ((digits != 1) ? " digits, " : " digit, ")
+ whitespaces
+ ((whitespaces != 1) ? " whitespaces, " : " whitespace, ")
+ consonants
+ ((consonants != 1) ? " consonants. " : " consonant."));
System.out
.print("Would you like to check another phrase in the Vowel Counter? if so Press 1 if not press any other key... ");
String tempNum = input.next();
try
{
answer = Integer.parseInt(tempNum);
}
catch (NumberFormatException e)
{
answer = 0;
}
System.out.println();
} while (answer == 1);
input.close();
System.out.println("Have a nice day");
}
当你输入一些字符串时:&#34; dfkl; kg-0sdf903o4k; l5kw34 -0 kdfo0orterl; t34k34l; k5l; 34&#34;,您得到以下输出:
ERROR: Not an alphanumeric character or a whitespace: ';'
ERROR: Not an alphanumeric character or a whitespace: '-'
ERROR: Not an alphanumeric character or a whitespace: ';'
ERROR: Not an alphanumeric character or a whitespace: '-'
ERROR: Not an alphanumeric character or a whitespace: ';'
ERROR: Not an alphanumeric character or a whitespace: ';'
ERROR: Not an alphanumeric character or a whitespace: ';'
The string: "dfkl;kg-0sdf903o4k;l5kw34 -0 kdfo0orterl; t34k34l;k5l;34 " has 4 vowels, 17 digits, 4 whitespaces, 25 consonants.
答案 3 :(得分:0)
// counting vowels consonants and blank spaces from a string
import java.util.*;
class VowConsBlank
{
public static void main(String s[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string");
String str=sc.nextLine();
int c=0,v=0,b=0;
for(int i=0;i<str.length();i++)
{
if("AEIOUaeiou ".indexOf(str.charAt(i))==-1)
c++;
else if(str.charAt(i)==' ')
b++;
else
v++;
}
System.out.println(" No of vowels: "+v+"\n No. of consonants: "+c+"\n
No. of blank: "+b);
}