面向对象

时间:2014-04-23 12:57:00

标签: java arrays object

我有一个关于面向对象的项目,需要几周时间,我完全清楚这是'家庭作业'。我真的陷入了这些错误,我根本无法理解它们!我得到的错误是说我没有while循环,非法启动表达并在解析时到达文件结尾。如果你们能帮我一点,我真的很感激!谢谢!

到目前为止,这是我的代码:

{

public static void main(String[] args)

{   
    int choice;
    int index;
    String name = " ";
    int goals;
    int points;
    int noofthings;
    int location;
    Footballer tempObject;
    String search = "";
    int step;

    noofthings = 0;

    do{
            choice = menu();

            switch(choice)
        {
                    case 1 :
                {
                    step = 0;
                    System.out.print("Enter a name ");
                    name = EasyIn.getString();

                    while((step < noofthings) && !(names[step].getName().equals(name)))
                        {
                            step++;
                        }
                    if(step < noofthings)
                        {
                            System.out.println("Name already exists, choose another ");
                        }
                    else
                        {
                            System.out.println("Enter amount of goals ");
                            goals = EasyIn.getInt();
                            System.out.println("Enter amount of points ");
                            points = EasyIn.getInt();
                            tempObject = new Footballer(name, goals, points);
                            location = findPlace(names, tempObject, noofthings);
                            noofthings = addOne(names, location, tempObject, noofthings);
                        }
                }
        }
    }
}

}

这是足球运动员班:

足球运动员 {

private String name;
private int goals;
private int points;


public Footballer () // constructor

{

}

public Footballer (String theName, int theGoals, int thePoints) // constructor

{
    name = theName;
    goals = theGoals;
    points = thePoints; 
}

// Mutators (Setter Methods)

public void setName (String theName)

{
    name = theName;
}

public void setGoals (int theGoals)

{
    goals = theGoals;
}

public void setPoints (int thePoints)

{
    points = thePoints;
}


// Selectors (Getter Methods) no parameters

public String getName ()

{
    return name;
}

public int getGoals()

{
    return goals;
}

public int getPoints ()

{
    return points;
}


public String toString ()

{
    String myString;

    myString = "";
    myString += "\n Name            : " + name;
    myString += "\n Goals Scored    : " + goals;
    myString += "\n Points Scored   : " + points;
    myString += "\n Total Points    : " + totalPoints() + "\n"; //prints the total points scored using the method totalPoints()

    return myString;

} 

//method to calculate the total points for a player
public int totalPoints()

{
    return goals * 3  + points;
}

}

这是一个问题:(可能会让它更容易一些)

****玩家排名系统 - 对象数组

在此项目中,您需要编写一个包含3个菜单选项的程序 1.添加播放器 2.删除播放器 3.列出所有玩家 4.退出 要做到这一点,您需要使用存储在公共驱动器上的足球运动员类作为Footballer.java。您还需要复制文件FootballerUse14.java并将其修改并将其作为项目文件提交,方法是将其存储在M驱动器上。如果您的文件未保存为FootballerUse14.java,则不会收集该文件,因此无法获得标记。注意 - 您无法以任何方式对足球运动员进行选择。 在程序中,您需要编写方法addone()deleteOne()findPlace()和listAll()等。足球运动员对象将根据通过调整totalPoints方法降序计算的值添加到足球运动员对象的数组中,即总点数最高的足球运动员将首先出现在阵列中(即指数位置为零)。在向阵列中添加足球运动员对象时,首先要求用户输入足球运动员名称,如果该名称已存在,则该程序不允许您添加足球运动员,而是给您一条消息,指示“抱歉此足球运动员名称已存在”。如果它不存在,则允许用户继续并输入其余数据。类似地,当从阵列中删除足球运动员对象时,程序应该询问你足球运动员的名字,如果它存在则被删除,如果不是你的程序输出给足够的足球运动员不存在的话。


1 个答案:

答案 0 :(得分:2)

如果您使用关键字do{,则必须包含while关键字才能定义,直到do{while之间指定的代码执行操作为止

解决方案包括while这样的条件

do{
//do your stuff here... switch etc.

}while(condition) //condition must return boolean value or be boolean expression

我看到你的while there因此错误可能是错误的{}展示位置

编辑:

我将您的代码复制粘贴在Netbeans中,是的,您的问题是do{}缺少while声明

添加时继续使用您的工作代码
public static void main(String[] args)

{   
    int choice;
    int index;
    String name = " ";
    int goals;
    int points;
    int noofthings;
    int location;
    Footballer tempObject;
    String search = "";
    int step;

    noofthings = 0;

    do{
            choice = menu();

            switch(choice)
        {
                    case 1 :
                {
                    step = 0;
                    System.out.print("Enter a name ");
                    name = EasyIn.getString();

                    while((step < noofthings) && !(names[step].getName().equals(name)))
                        {
                            step++;
                        }
                    if(step < noofthings)
                        {
                            System.out.println("Name already exists, choose another ");
                        }
                    else
                        {
                            System.out.println("Enter amount of goals ");
                            goals = EasyIn.getInt();
                            System.out.println("Enter amount of points ");
                            points = EasyIn.getInt();
                            tempObject = new Footballer(name, goals, points);
                            location = findPlace(names, tempObject, noofthings);
                            noofthings = addOne(names, location, tempObject, noofthings);
                        }
                }
        }
    }while(variable!=anotherVariable); //added this line
}