-1

It is supposed calculate the sales commission based on input variables and list them after the loop is done.

I am unsure how to go about listing the values of totalEarned after because they change when the loop is done.

import java.util.Scanner;

public class SalesCommissionCalc

{
 public static void main( String [] args )
  {
     final int SENTINEL = -1;
     double grossSales;
     double totalEarned;
     Scanner scan = new Scanner( System.in );

     System.out.print( "Enter gross sales for the last week, or -1 to stop > " );
     grossSales = scan.nextDouble( );

     while ( grossSales != SENTINEL )
 {
         totalEarned =(500 + (grossSales * .08));
         System.out.println( "Earned last week: $" + totalEarned );

         System.out.print( "Enter gross sales for the last week, or -1 to stop > " );
         grossSales = scan.nextDouble( );
         System.out.println( "Total Earned: $" + totalEarned );
     }

  }
}

Also if I added names to the program would I have to introduce another loop or would I be able to squeeze it into the same loop and keep the inputted names attached to the inputted values for when I list them after the loop?

jimjab
  • 9
  • 2

2 Answers2

0

You could save the values in a list and then retrieve and use them in the way you want to.

import java.util.Scanner;

public class SalesCommissionCalc  
{
 public static void main( String [] args )
 {
     final int SENTINEL = -1;
     double grossSales;
     double totalEarned;
     //declare the list
     List<double> earnings = new List<double>;
     Scanner scan = new Scanner( System.in );

     System.out.print( "Enter gross sales for the last week, or -1 to stop > " );
     grossSales = scan.nextDouble( );

     while ( grossSales != SENTINEL )
     {
         totalEarned =(500 + (grossSales * .08));
         //add the value to the list
         earnings.add(totalEarned);
         System.out.println( "Earned last week: $" + totalEarned );

         System.out.print( "Enter gross sales for the last week, or -1 to stop > " );
         grossSales = scan.nextDouble( );
         System.out.println( "Total Earned: $" + totalEarned );
     }

     //retrieve the value at the third position in the list
     earnings.get(3); //here you could also use a foreach or for loop to iterate through all list entries
  }
}
Geshode
  • 3,600
  • 6
  • 18
  • 32
0

You must retrieve the values by passing them in a list or an array.

List<double> earned = new List<double>;