我正在尝试让Z
的下一个字母成为A
,但是我的if else语句无法正常工作
import java.util.ArrayList; // import the ArrayList class
public class Main {
public static void main(String[] args) {
ArrayList<String> alphabets = new ArrayList<String>(); // Create an ArrayList object
String getalpha;
alphabets.add("A");
String nextalpha;
String lastchar;
int charValue;
int x = 0;
while (x < 30) {
getalpha = alphabets.get(x);
lastchar = getalpha.substring(getalpha.length() - 1);// get last character in alphabets
if (lastchar != "Z") {
charValue = lastchar.charAt(0);
nextalpha = String.valueOf((char) (charValue + 1));// next alphabets
} else if (lastchar == "Z") {
nextalpha = "A";
}
getalpha = getalpha + nextalpha;
alphabets.add(getalpha);
x++;
System.out.println(getalpha);
}
// System.out.println(alphabets);
}
}
最佳答案
您不能将字符串与“ ==”运算符进行比较,因为它会检查相等的引用而不是值。使用equals函数。
String a = "z";
String b = "z";
System.out.println(a==b); // false
System.out.println(a.equals(b)); // true