简体   繁体   中英

Why does the code print the statement twice, but differently?

My problem statement is:

Write a program that creates two instances of the generic class LinkedList.
The first instance is stadiumNames and will hold items of type String.
The second instance is gameRevenue and will hold items of type Double.
Within a loop, read data for the ball games played during a season.
The data for a game consists of a stadium name and the amount of money made for that game.
Add the game data to stadiumNames and gameRevenue.
Since more than one game could be played at a particular stadium, stadiumNames might have duplicate entries.
After reading the data for all of the games, read a stadium name and display the total amount of money made for all the games at that stadium.

I'm trying to get each input from the user and then add each input together and get its sum, it seems to get it right at first, but then it prints another totally different amount. Why is that? Any help appreciated.

Each input the stadiumName and gameRevenue were added to a linkedList .

Note that I already wrote both linked lists but it won't allow me to post a big chunk of code. Thank you.

boolean Data = true;
while (Data) {
    stadiumNames.add(name);
    gameRevenue.add(rev);
    System.out.println("Do you want another game? ");
    String yesorno = scan.next();
    if (yesorno.equals("No"))
        break;
    else {
        if (yesorno.equals("yes"))
            System.out.println("Enter stadium name: ");
        name = scan.next();
        System.out.println("Enter amount of money for the game: ");
        rev = scan.nextDouble();
        for (int i = 0; i < stadiumNames.size(); i++) {
            if (stadiumNames.get(i).equals(name)) {
                rev += gameRevenue.get(i);
                System.out.println("The total amount of money for " + name + " is " + rev);
            }
        }
    }
}

输出的附加图像

If you want to print running total while user is entering the data, total should be reset for each calculation.


while (true) {
    System.out.println("Do you want another game? ");
    String yesorno = scan.next();
    if (yesorno.equals("No"))
        break; // else not needed

    System.out.println("Enter stadium name: ");
    name = scan.next();
    System.out.println("Enter amount of money for the game: ");
    rev = scan.nextDouble();

    stadiumNames.add(name);
    gameRevenue.add(rev);

    double total = 0.0;

    // recalculating the total for the last stadium
    for (int i = 0; i < stadiumNames.size(); i++) {
        if (stadiumNames.get(i).equals(name)) {
            total += gameRevenue.get(i);
        }
    }
    System.out.println("The total amount of money for " + name + " is " + total);
}

However, it may be needed to calculate the totals for multiple different stadiums and a map needs to be created and filled for this after the while loop.
It is convenient to use Map::merge function to accumulate the totals per stadium name.

Map<String, Double> totals = new LinkedHashMap<>();
for (int i = 0; i < stadiumNames.size(); i++) {
    totals.merge(stadiumNames.get(i), gameRevenue.get(i), Double::sum);
}
totals.forEach((stad, sum) -> System.out.println("The total amount of money for " + stad + " is " + sum));

Aside comment: it is not recommended to use double for financial calculations because floating point maths is not precise .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM