我正在尝试将元素输入到具有未知元素值或未知数量的元素的数组中
public class Sales
{
public void salesAmount()
{
Scanner scan = new Scanner(System.in);
int sum = 0;
int salespeople = 0;
//Create printstatement to ask the user to ("enter the number of sales people");
System.out.println("Enter the number of sales people: ");
//store the response in salespeople
salespeople = scan.nextInt();
//Create an int array called sales that will have the number entered by user as the number of elements
int[] sales = new int[salespeople];
//create a for loop that will loop through the array sales.
for(int i = 0; i < salespeople; i++)
{
//Create print statement that says: ("Enter sales for salesperson " + i + ": ");
System.out.println("Enter sales for salesperson " + i + ": ");
sales[i] = scan.nextInt();
//store response in sales[i]
sum = sum + sales[i];
}
System.out.println("Salesperson\tSales");
System.out.println("--------------------");
for (int i=0; i<sales.length; i++)
{
System.out.println(i + "\t" + sales[i]);
sum += sales[i];
}
System.out.println("\nTotal sales: " + sum);
}
}
我将其全部打印在第二个文件上,代码如下:
public static void main(String[]args)
{
Sales sales = new Sales();
sales.salesAmount();
}
预期的结果应该是所有输入数字的总和,但我的结果是它们的总和然后加倍。
慕慕森
慕田峪9158850
相关分类