Rewrite using the Scanner class and a while loop import java.text.DecimalFormat;
ID: 3766784 • Letter: R
Question
Rewrite using the Scanner class and a while loop
import java.text.DecimalFormat;
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?");
days = Integer.parseInt(input);
totalSales = 0.0;
for (int count = 1; count <= days; count++)
{
input = JOptionPane.showInputDialog("Enter the sales " + "for day " + count + ":");
sales = Double.parseDouble(input);
totalsales += 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.