简体   繁体   中英

How to get series of data using user input in Java

I am trying to get series of user input and from them I need to get this kind of output, I tried various methods but didn't work.

Day      Sales
Sunday   $ 0.00
Monday   $ 4,300.76
Tuesday  $ 276.92
Wednesday$ 15,976.43
Thursday $ 0.00
Friday   $ 49,764.67
Saturday $ 250.00
-----------
Total Sales: $ 70,568.78
Average sale value: $2,367.92
Commission on Sales: $7,551.19 ***

I am trying using this, But it gives me errors and I don't have idea to get that kind of output. And also I used list but how can I separate the values?

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    do
    {
        System.out.println("Enter Day of Sale (Sunday, Monday, Tuesday, etc.): ");
        String text = br.readLine();
        System.out.println("Enter Sale Amount: ");
        int a  = Integer.parseInt(br.readLine(), 7);
        System.out.println("Are you finished entering sales? (Y/N)");

        System.out.println(text);
        System.out.println(a);
        if (br.readLine().startsWith("y"))
        {
        } else
        {
            break;
        }
    } while (true);
    br.close();

How can I do that? Thanks in advance.

tl;dr - Introduce object which will hold data, and add object's to List. To display, iterate thru List.

 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 List<Entry> collectedData = new ArratList<>();

    do
    {
        Entry entry = new Entry();
        System.out.println("Enter Day of Sale (Sunday, Monday, Tuesday, etc.): ");
        entry.dayName = br.readLine();
        System.out.println("Enter Sale Amount: ");
        entry.value  = Integer.parseInt(br.readLine(), 7);
        collectedData.add(entry);
        System.out.println("Are you finished entering sales? (Y/N)");

        System.out.println(text);
        System.out.println(a);
        if (br.readLine().startsWith("y"))
        {
        } else
        {
            break;
        }
    }

for(entry : collectedData) {
    System.out.println(entry.dayName + ": " + entry.value);
}

public class Entry {
    String dayName;
    Integer value;
}

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