Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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);
     }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote