これは、私のプログラムで探している出力の例です。
出力例
各行にスタジアム名とゲームの収益を入力します
終了したら 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();
}
}