在对象中苦苦挣扎,程序无法按预期工作(java)

时间:2014-10-24 09:20:25

标签: java object-reference

所以我有这个骑自行车的游戏,我希望按名字显示骑手名单,然后我想在用户选择骑手时显示他们的能力。该程序编译和运行很好,问题出在我的riders()方法。它只是不打印出我的第一个骑手c1。提前谢谢。

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class CyclingManager2 {
public static void main(String[] args) {

    //menyvalgene
    Menu m = new Menu();

    m.choice();


    }
}

class Menu {
Scanner in = new Scanner(System.in);
Cyclist cy = new Cyclist();

//choices
public void choice() {

    int choice = -1;
    while (choice != 0) {

        System.out.println("Choose something: ");
        System.out.println("-0 will exit the program" + "\n-Pressing 1 will open the database menu");
        choice = in.nextInt();
        switch(choice) {
            case 0: choice = 0; break;
            case 1: database(); break;
            default: System.out.println("You have to choose either 0 or 1"); break;
            }

        }
    }
public void database() {

    System.out.println("Welcome to the database \nThese are the options:\n0 = Back to menu\n1: Riders");
    int dbChoice = -1;

    while (dbChoice != 0) {

        System.out.println();
        dbChoice = in.nextInt();
        switch(dbChoice) {
            case 0: dbChoice = 0; break;
            case 1: cy.riders(); System.out.println("Press 0 for going back to the menu\nPress 1 for showing the riders");break;
            default: System.out.println("Choose either 0 or 1"); break;
        }

    } 
}

}

class Cyclist {

List<Cyclist> cyclists = new ArrayList<>(); 
private String name;
private int mountain;
private int timeTrial;
private int sprint;
private int age;
Cyclist c1 = null;


public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void setMountain(int mountain) {
    this.mountain = mountain;
}

public int getMountain() {
    return mountain;
}


public void setTimeTrial(int timeTrial) {
    this.timeTrial = timeTrial;
}

public int getTimeTrial() {
    return timeTrial;
}

public void setSprint(int sprint) {
    this.sprint = sprint;
}

public int getSprint() {
    return sprint;
}

public void setAge(int age) {
    this.age = age;
}

public int getAge() {
    return age;
}


public void riders() {
    abilities();
    for (int i = 0; i < cyclists.size(); i++){
          System.out.print(((Cyclist) cyclists).getName());
    }

}



public void abilities() {

    //Pardilla is created

    c1 = new Cyclist();
    c1.setName("Sergio Pardilla");
    c1.setMountain(75);
    c1.setTimeTrial(60);
    c1.setSprint(60);
    c1.setAge(30);

    /*System.out.println(c1.getName() + "'s abilities:");
    System.out.println("Mountain - " + c1.getMountain());
    System.out.println("TimeTrial - " + c1.getTimeTrial());
    System.out.println("Sprint - " + c1.getSprint());
    System.out.println("Age - " +c1.getAge());

    cyclists.add(c1); //adds Pardilla to cyclists arraylist*/
    }

}

4 个答案:

答案 0 :(得分:2)

我想你想要更多的东西:

public void riders() {
    abilities();
    for (int i = 0; i < cyclists.size(); i++){
          System.out.print(cyclists.get(i).getName());
    }
}

另一件事是,我不确定你是否希望List<Cyclist> cyclists = new ArrayList<>();加入Cyclist班级。

答案 1 :(得分:2)

你有这个for循环:

for (int i = 0; i < cyclists.size(); i++) {
    System.out.print(((Cyclist) cyclists).getName());
}

它不会起作用。您正在将整个cyclistsArrayList)投射到一个cyclist实例。您可能希望迭代ArrayList的内容并获取Cyclist数组中的每个cyclists - 对象。尝试一个foreach循环:

for (Cyclist cyclist : cyclists) {
    System.out.print(cyclist.getName());
}

或使用带有索引检索的for循环:

for (int i = 0; i < cyclists.size(); i++) {
    cyclists.get(i).getName());
}

答案 2 :(得分:1)

两个问题:

  1. 将您的骑手添加到ArrayList的部分已注释掉。
  2. 循环ArrayList的方式绝对不正确。试试这样:

    for (Cyclist cyclist : cyclists) {
        System.out.println(cyclist.getName());
    }
    

答案 3 :(得分:0)

您应该通过拨打号码来获取列表中的对象:cyclists.get(i).getName())