1

这是我正在为我的程序寻找的示例输出。

示例输出

在每一行输入一个球场名称和比赛收入

完成后输入done

巨人1000

福克斯波罗 500

巨人1500

完毕

输入体育场的名称以获得总收入:

巨人队

总收入为 2500.00

我得到了一切工作,但最后的总收入。我不知道如何进入我的链表并获取游戏收入并显示它..这是我现在的代码,我一直在搞乱......

import java.util.LinkedList;
import java.util.Scanner;


public class LinkedLists {

    private final Scanner keyboard;



    private final LinkedList<String> stadiumNames;
    private final LinkedList<Integer> gameRevenue;


    public LinkedLists() {
        this.keyboard = new Scanner(System.in);
        this.stadiumNames = new LinkedList<String>();
        this.gameRevenue = new LinkedList<Integer>();
    }

    public void addData(String stadium, int revenue){
        stadiumNames.add(stadium);
        gameRevenue.add(revenue);
    }

    public void loadDataFromUser() {
        System.out.println("On each line enter the stadium name and game revenue.");
        System.out.println("Enter done when you are finished.");

        boolean done = false;
        while(!done) {
            System.out.print("Enter the name of the stadium:");
            String stadium = keyboard.next();
            if (stadium.equals("done")) {
                done = true;
            } else {
                System.out.print("Enter game revenue: ");
                addData(stadium, keyboard.nextInt());
            }
        }
    }

    // return -1 if not found
    public int getIndexForName(String name) {

        if(stadiumNames.contains(name)){
            System.out.println("The total revenue is: " +gameRevenue.get(0));
            System.exit(0);
        }

        return -1;

    }


    public void showInfoForName(String name) {
        int index = getIndexForName(name);
        if (index==-1) {
            System.out.println("There is no stadium named " + name);
        } else {

        }
    }

    public void showInfoForName() {
        System.out.println("Enter the name of the stadium to get the total revenue for it.");
        showInfoForName(keyboard.next());
    }

    public static void main(String[] args) {
        LinkedLists pgm = new LinkedLists();
        pgm.loadDataFromUser();
        pgm.showInfoForName();
    }
}
4

2 回答 2

3

试试这个:

public int getIndexForName(String name) {
    if (stadiumNames.contains(name)) {
        int total = 0;
        for (int i = 0 ; i < stadiumNames.size() ; i++)
            if (stadiumNames.get(i).equals(name))
                total += gameRevenue.get(i);
        System.out.println("The total revenue is: " + total);
        System.exit(0);
    }
    return -1;
}

不过,有更好的方法可以做到这一点。我可能会使用Map将每个体育场名称映射到其总收入的方法。addData您可以在方法中更新此地图。

于 2012-09-08T19:30:14.857 回答
0

使用哈希映射更简洁,而且您可以将总数保存在一边而不计算它:

import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;

/**
 * User: user
 * Date: 09/09/2012
 */
public class Blah {

    private Scanner keyboard;
    private Map<String, Integer> stadiumRevenues;
    private Integer total = 0;

    public Blah() {
        this.keyboard = new Scanner(System.in);
        this.stadiumRevenues = new HashMap<String, Integer>();
    }

    public void addData(String stadium, int revenue){
        stadiumRevenues.put(stadium, revenue);
        total += revenue;
    }

    public void loadDataFromUser() {
        System.out.println("On each line enter the stadium name and game revenue.");
        System.out.println("Enter done when you are finished.");

        boolean done = false;
        while(!done) {
            System.out.print("Enter the name of the stadium:");
            String stadium = keyboard.next();
            if (stadium.equals("done")) {
                done = true;
            } else {
                System.out.print("Enter game revenue: ");
                addData(stadium, keyboard.nextInt());
            }
        }
    }

    public int getTotal() {
        return total;
    }

    // return -1 if not found
    public int getIndexForName(String name) {

        Integer rev = stadiumRevenues.get(name);

        if(rev != null){
            System.out.println("The total revenue is: " +rev);
            System.exit(0);
        }

        return -1;

    }

    public void showInfoForName(String name) {

        Integer rev = stadiumRevenues.get(name);

        if (rev == null) {
            System.out.println("There is no stadium named " + name);
        } else {

        }
    }

    public void showInfoForName() {
        System.out.println("Enter the name of the stadium to get the total revenue for it.");
        showInfoForName(keyboard.next());
    }

    public static void main(String[] args) {
        Blah pgm = new Blah();
        pgm.loadDataFromUser();
        pgm.showInfoForName();
    }


}
于 2012-09-09T06:14:54.227 回答