应该根据输入变量计算销售佣金,并在循环完成后列出它们。
我不确定如何列出之后的totalEarned值,因为循环完成后它们会改变。
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 );
}
}
}
如果我在程序中添加了名称,是否还必须引入另一个循环,还是可以将其压缩到同一个循环中,并在循环后列出它们时将输入名称附加到输入值上? / p>
答案 0 :(得分:0)
您可以将值保存在列表中,然后以所需的方式检索和使用它们。
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
}
}
答案 1 :(得分:0)
您必须通过将它们传递到列表或数组中来检索这些值。
List<double> earned = new List<double>;