Using java modify the code below using the Scanner class and a while loop. impor
ID: 3766786 • Letter: U
Question
Using java modify the code below using the Scanner class and a while loop.
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
/**
This program calculates a running total.
*/
public class TotalSales
{
public static void main(String[] args)
{
int days; //The number of days
double sales; // A day's sales figure
double totalSales; //Accumulator
String input; //To hold the user's input
//Create a DecimalFormat object to format output.
DecimalFormat dollar = new DecimalFormat("#,##0.00");
//Get the number of days.
input = JOptionPane.showInputDialog("For how many days " + "do you have sales figures?");
days = Integer.parseInt(input);
//Set the accumulator to 0.
totalSales = 0.0;
//Get the sales figures and clculate a running total.
for (int count = 1; count <= days; count++)
{
input = JOptionPane.showInputDialog("Enter the sales " + "for day " + count + ":");
sales = Double.parseDouble(input);
totalSales += sales; //Add sales to totalSales.
}
//Display the total sales.
JOptionPane.showMessageDialog(null, "The total sales are $" + dollar.format(totalSales));
System.exit(0);
}
}
Explanation / Answer
import java.text.DecimalFormat;
import java.util.*;
import javax.swing.JOptionPane;
/**
*/
public class TotalSales
{
public static void main(String[] args)
{
int days;
double totalSales;
String input;
DecimalFormat dollar = new DecimalFormat("#,##0.00");
//input = JOptionPane.showInputDialog("For how many days " +
// "do you have sales figures?");
Scanner in = new Scanner(System.in);
System.out.println("For how many days " + "do you have sales figures?");
days = in.nextInt();
totalSales = 0.0;
int count=1;
while(count<=days)
{
input = JOptionPane.showInputDialog("Enter the sales " + "for day " + count + ":");
sales = Double.parseDouble(input);
totalsales += sales;
count++;
}
JOPtionPane.showMessageDialog(null, "The total sales are $" + dollar.format(totalSales));
System.exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.