47.5k views
5 votes
Write Java statements to determine and display the highest sales value, and the

month in which it occurred, and the lowest sales value and the month in which it
occurred.. Use the JOptionPane class to display the output.

1 Answer

4 votes

Answer:

import javax.swing.JOptionPane;

public class SalesReport {

public static void main(String[] args) {

// initialize the sales and months arrays

double[] sales = {1000, 1200, 800, 1500, 900};

String[] months = {"January", "February", "March", "April", "May"};

// find the highest and lowest sales values and their corresponding months

double highestSales = sales[0];

String highestMonth = months[0];

double lowestSales = sales[0];

String lowestMonth = months[0];

for (int i = 1; i < sales.length; i++) {

if (sales[i] > highestSales) {

highestSales = sales[i];

highestMonth = months[i];

}

if (sales[i] < lowestSales) {

lowestSales = sales[i];

lowestMonth = months[i];

}

}

// display the results using JOptionPane

JOptionPane.showMessageDialog(null, "Highest sales: " + highestSales + " in " + highestMonth);

JOptionPane.showMessageDialog(null, "Lowest sales: " + lowestSales + " in " + lowestMonth);

}

}

Step-by-step explanation:

Assuming that the sales values are stored in an array named sales and the corresponding months are stored in an array named months, the following Java statements can be used to determine and display the highest and lowest sales values and their corresponding months using the JOptionPane class.

In this example, the program initializes the sales and months arrays with some sample data. Then, it uses a for loop to iterate over the sales array and update the highestSales, highestMonth, lowestSales, and lowestMonth variables as needed. Finally, the program displays the results using the JOptionPane.showMessageDialog() method.

User Brunno
by
7.5k points