gpt4 book ai didi

java - toUpperCase(); - 异常 : The method toUpperCase() is undefined for the type Pseudo

转载 作者:行者123 更新时间:2023-12-01 11:02:35 24 4
gpt4 key购买 nike

这是我的程序的代码。主要方法已提供,不可触及。 main 方法将 String 转换为大写,但不接受 toUpperCase(); java方法。如何在不改变 main 方法的情况下解决这个问题?

public class Pseudo {

String name;
char charArray[] = new char[80];
int lenghtOfString =0;
String sArray;

public static void main(String[] args) {

// Instantiate class
Pseudo pseudo = new Pseudo();

// Verify class
System.out.println("Verifying Pseudo:");
pseudo.setString("1234567890 Computer Science !(*@&#)*&");
System.out.println("pseudo = " + pseudo.getString());
System.out.println("pseudo.charAt(5) = " + pseudo.charAt(5));
System.out.println("pseudo.indexOf('C') = " + pseudo.indexOf('C'));
System.out.println("pseudo.toUpperCase() = " + pseudo.toUpperCase());
System.out.println("pseudo.substring(11, 19) = " + pseudo.substring(11, 19));
pseudo.setString("Compare");
System.out.println("pseudo.equals(\"Compare\") = " + pseudo.equals("Compare"));
System.out.println("pseudo.equals(\"Compare!\") = " + pseudo.equals("Compare!"));

// String equivalent (should match!)
System.out.println("\nComparing String:");
String string = "1234567890 Computer Science !(*@&#)*&";
System.out.println("string = " + string);
System.out.println("string.charAt(5) = " + string.charAt(5));
System.out.println("string.indexOf('C') = " + string.indexOf('C'));
System.out.println("string.toUpperCase() = " + string.toUpperCase());
System.out.println("string.substring(11, 19) = " + string.substring(11, 19));
string = "Compare";
System.out.println("string.equals(\"Compare\") = " + string.equals("Compare"));
System.out.println("string.equals(\"Compare!\") = " + string.equals("Compare!"));
}

public void setString(String s1){
name=s1;
}
public String getString() {
return name;
}
public char charAt(int i1) {
return name.charAt(i1);
}
public int indexOf(char c1) {
return name.indexOf(c1);
}
public String substring(int start, int end) {
return name.substring(start,end);
}
public boolean equals(String s2) {
if (s2 == name){
return true;
}
else {
return false;
}
}
}

我知道我可以创建一个名为 toUpperCase 的方法来运行 .toUpperCase 方法。但是你能解释一下为什么你必须创建一个方法来运行 .toUpperCase(); java方法已经在它的库中了吗?为什么 .toUpperCase(); 可以方法在 main 中工作不一样吗?

最佳答案

该错误是不言自明的。您的 Pseudo 类没有方法 toUpperCase()

因此,将方法 toUpperCase 添加到您的 Pseudo 类中:

public String toUpperCase()
{
return name.toUpperCase();
}

关于java - toUpperCase(); - 异常 : The method toUpperCase() is undefined for the type Pseudo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33225691/

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