我仍然遇到这个项目的问题。我一直找不到符号'在我的第三种方法中,以及它在足球运动员身上[]'部分!如果您可以帮助我,我们将不胜感激!
这是我的代码:
public static int addOne(String theArray[], int place, String theName, int noOfValues)
{
int step;
if (noOfValues == 0)
{
theArray [0] = theName;
noOfValues ++;
}
else
{
for (step = noOfValues - 1 ; step >= place; step --)
{
theArray[step + 1] = theArray[step] ;
}
theArray[place] = theName;
noOfValues ++;
}
return noOfValues;
}
public static int findPlace(String theArray[], String theName, int noOfValues)
{
int step;
int place;
step = 0 ;
while ((step < noOfValues) && (theName.compareTo(theArray[step]) > 0))
{
step ++;
}
place = step ; // Holds the correct location of place
return place; // method must return an int
}
public static void listAll(Footballer PlayerIn[], int noOfValues)//displays array contents
{
int index;
for(index =0; index <noOfValues; index++)
{
System.out.print(PlayerIn[index]);
}
}
public static void menu()
{
System.out.println(" Menu ");
System.out.println(" ---------- ");
System.out.println();
System.out.println(" 1. Add ");
System.out.println(" 2. Delete ");
System.out.println(" 3. List All ");
System.out.println(" 4. Exit ");
}
public static void options()
{
char options;
System.out.println();
System.out.print("Enter Option Required (1-4) ");
options = EasyIn.getChar();
while (options != '4')
{
switch(options)
{
case '1': System.out.println("Add Name");
EasyIn.pause();
break;
case '2': System.out.println("Delete Name");
EasyIn.pause();
break;
case '3': System.out.println("List All");
EasyIn.pause();
break;
default: System.out.println("Invalid Option!");
EasyIn.pause();
}
System.out.println();
System.out.println("Enter Option Required (1-4) ");
options = EasyIn.getChar();
}
}
public static void main(String[] args)
{
menu();
options();
}
}
答案 0 :(得分:0)
public static void listAll(Footballer[] footballers) {
// displays array contents
for (Footballer footballer : footballers) {
System.out.print(footballer);
}
}
使用该语言。你正试图像Java那样编写Java程序。你不必努力工作......
例如,所有数组都有一个成员(因为它们有点像对象)
Footballer[] footballers = ...;
int arraySize = footballers.length;
每个&#34;&#34;循环运算符使整个数组的行走变得轻而易举
for (Footballer footballer : footballers) {
...
}
此外,您的代码似乎是&#34;添加到位&#34;操纵数组。你通常不这样做,因为你的阵列不会增长,所以你将失去最后一个元素(或溢出)。在Java中,您使用List
进行处理。使用ArrayList
List
,其中存储是一个可调整大小的数组,用于执行malloc并在空间不足时进行复制。
List<Footballer> footballers = ...;
footballers.add(5, new Footballer("Tom the Leg"));
现在你已经有了一个add-at-index操作,但请记住,List的获取长度的方法很遗憾地被命名为不同的,footballers.size()
< / p>