gpt4 book ai didi

Java字符串数组长度错误

转载 作者:行者123 更新时间:2023-12-01 16:56:59 26 4
gpt4 key购买 nike

我是 Java 初学者,正在练习自己编写代码。我正在使用一本书来教我如何编码,并且我陷入了一个项目,其中包括创建字符串数组并确定其长度。这是我的示例工作。这是一个简单的婴儿名字建议程序。

public class BabyName {
public static void main(String[] args) {
String[] nameListOne = {"Alvin", "Elmer", "Angel", "Michael", "Tim", "Jude", "Gabriel", "Raphael", "John", "Smith", "Carl", "Mike"};
String[] nameListTwo = {"", "Mark", "Posh", "Jake", "Cloud", "Star", "Ben", "Sam", "Kim", "Mark", "Simeon", "Louie", "Nat", "Matt", ""};

int nameOneLength = nameListOne.Length;
int nameTwoLength = nameListTwo.Length;

int rand1 = (int) (Math.random() * nameOneLength);
int rand2 = (int) (Math.random() * nameTwoLength);

String babyname = nameListOne[rand1] + " " + nameListTwo[rand2];

System.out.println("Your suggested baby name is " + babyname);
}
}

当我尝试编译它时,我收到此错误:

BabyName.java:6: error: cannot find symbol
int nameOneLength = nameListOne.Length;
^
symbol: variable Length
location: variable nameListOne of type String[]
BabyName.java:7: error: cannot find symbol
int nameTwoLength = nameListTwo.Length;
^
symbol: variable Length
location: variable nameListTwo of type String[]
2 errors

我不知道我在这里做错了什么。我只是按照书上写的去做。

希望有人能帮忙。

最佳答案

nameListOne.length 长度应小写

关于Java字符串数组长度错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31240258/

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