【发布时间】:2022-01-13 21:00:26
【问题描述】:
我正在为我的 Java 类做一个作业,要求我编写一个程序来显示字符串中大写字母的数量。我在第 26 行遇到错误(for (int i = 0; i
import java.util.Scanner;
public class Uppercase{
public static void main(String[] args){
char[] newWord;
Scanner userWord = new Scanner(System.in);
System.out.println("Enter a word");
String word = userWord.nextLine();
System.out.println("There are " + newWord.numUppercase(word) + "uppercase letters");
}
public int numUppercase(String s){
char[] ch = new char[s.length()];
for (int i = 0; i < s.length(); i++){
ch[i] = s.charAt(i);
int uppercase = 0;
for (int i = 0; i < ch[].length; i++){
if(ch[i].valueOf() > 64 && ch[i].valueOf() < 91){
uppercase++;
}
}
return uppercase;
}
}
}
【问题讨论】:
-
ch是数组的名称。当您添加[]运算符时,您试图将char从数组中拉出,而char没有length字段。