2012-09-08 67 views
1

這是我正在尋找的示例輸出,用於我的程序。如何從LinkedList中獲取數據?

示例輸出

在各行中輸入球場名稱和遊戲收入

輸入您在完成

巨人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(); 
    } 
} 
+0

你有使用鏈表? – arshajii

+0

誰來回答這個問題? –

+0

是的,我有一個鏈接列表 –

回答

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方法中更新此地圖。

+0

老兄....我愛你,它的工作原理!我得研究這個。 –

+0

我很高興我能幫上忙。不要忘記接受; D另外,如果事情是這樣,你可能想把你的問題標記爲家庭作業。 – arshajii

+0

會做,非常感謝 –

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(); 
    } 


}